In java, does run()
register a thread in a thread scheduler?
What abo开发者_Go百科ut construct()
,start()
and register()
?
In java, does run() register a thread in a thread scheduler?
No. If you call the run()
method directly, it is called as a normal method; i.e. it runs on the current thread, not a new one.
What about construct(),start() and register()
The start
method creates a new thread, and in the process the thread will be registered with the scheduler. (However, the scheduler is a nebulous concept in Java. It is implied that one must exist, but its implementation and behavior are typically left to the host operating system. A pure Java program has almost no control over the way that the thread scheduler actually works.)
There are no construct()
or register()
methods in the Thread
API. If you are referring to the Thread
constructors, they only create a Thread
object, and NOT the underlying thread that will do the work. The latter is only created when start()
is called.
run()
is the actual code in the thread; so you could do like:
Thread childThread = new Thread() {
public void run() {
// do stuff on a new thread
}
};
(Though I've been told extending Thread like that is ugly ;)
So calling run() itself won't create a new thread. To do that, you use start():
childThread.start();
So, I guess it does give the scheduler a new thread to deal with -- but that's way down on the OS level.
I'm not sure what you mean by construct()
and register()
though?
精彩评论