In java, does run()
register a thread in a thread scheduler?
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.
Threads are scheduled for execution based on their priority. Even though threads are executing within the runtime, all threads are assigned processor time slices by the operating system. The details of the scheduling algorithm used to determine the order in which threads are executed varies with each operating system.
As mentioned briefly in the previous section, many computer configurations have a single CPU. Hence, threads run one at a time in such a way as to provide an illusion of concurrency. Execution of multiple threads on a single CPU in some order is called scheduling.
NT's thread scheduler, called the dispatcher by NT's developers, resides in the kernel.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With