简体   繁体   English

线程类的start()方法在实现线程类时如何调用子类的run()方法

[英]how does start() method of thread class call run() method of child class when it implements thread class

How does the start() method of Thread class call the run() method of a child class which implements the Thread class? Thread类的start()方法如何调用实现Thread类的子类的run()方法?

I know when we implement Runnable we pass the child class object to the Thread class constructor which has parameter of Runnable . 我知道当我们实现Runnable我们将子类对象传递给具有Runnable参数的Thread类构造函数。 But in the other case we call the start() method, and when the JVM doesn't find start() in the child class, it goes to Thread . 但是在另一种情况下,我们调用start()方法,并且当JVM在子类中找不到start()时,它将转到Thread Now to call run() , or pass it to the JVM, we need child class reference. 现在调用run()或将其传递给JVM,我们需要子类引用。 How does Thread implement it? Thread如何实现它?

That's an interesting thing to know. 这是一件有趣的事情。

Concurrency is achieved by the JVM, but to have it call run() of the child we need its reference or address. 并发是通过JVM实现的,但是要让它调用子代的run() ,我们需要它的引用或地址。

The start() method of a Thread instance does not call the run() method. Thread实例的start()方法不会调用run()方法。 What it does is, it creates a new operating system thread, and it makes arrangements for the run() method to be called in that new thread.* 它的作用是创建一个新的操作系统线程,并安排该新线程中调用run()方法。

Meanwhile, the t.start() potentially can return before the other thread enters t.run() . 同时, t.start()可能可以在另一个线程进入t.run()之前返回。


* How it makes those "arrangements" can be completely different from one operating system to another, but generally, it involves making a system call and passing a pointer to a native function that the OS will call. *如何进行这些“安排”在一个操作系统与另一个操作系统之间可能是完全不同的,但是通常,它涉及进行系统调用以及将指针传递给OS将要调用的本机函数。 The native function most likely will use the Java Invocation API , to get into Java library code that eventually will call t.run() . 本机函数很可能将使用Java Invocation API进入Java库代码,该代码最终将调用t.run()

As noted, Thread.start invokes a native method, which in turn invokes Thread.run : 如前所述, Thread.start调用一个本地方法,该方法又调用Thread.run

public synchronized void start() {
    startImpl();
}

private native void startImpl(); 

And the code for Thread.run has: Thread.run的代码具有:

public void run() {
    if (runnable != null) {
        runnable.run();
    }
}

Here, runnable is either null or is the Runnable instance which was provided when the thread was created. 在这里, runnablenull或创建线程时提供的Runnable实例。 The link to the runnable instance is through the reference which was provided to the thread constructor, and which is held as an instance value of the thread object. 到可运行实例的链接是通过提供给线程构造函数的引用进行的,该引用被保存为线程对象的实例值。

The key behavior is provided by the run method, which must either be implemented directly on the thread, or must be implemented on the runnable object which was provided to the thread constructor. 关键行为由run方法提供,该方法必须直接在线程上实现,或者必须在提供给线程构造函数的可运行对象上实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM