Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread scheduler registration?

In java, does run() register a thread in a thread scheduler?

What about construct(),start() and register() ?

like image 332
Nikhil Avatar asked Aug 22 '11 06:08

Nikhil


People also ask

Which method registers thread in thread scheduler?

The start method creates a new thread, and in the process the thread will be registered with the scheduler.

How do threads get scheduled?

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.

What happened thread scheduling?

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.

Where is thread scheduler present?

NT's thread scheduler, called the dispatcher by NT's developers, resides in the kernel.


2 Answers

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.

like image 188
Stephen C Avatar answered Sep 28 '22 15:09

Stephen C


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?

like image 27
Owen Avatar answered Sep 28 '22 13:09

Owen