Google

Jun 13, 2014

What does Java thread join do? What is the difference between start and run methods? What is a daemon thread? How are threads prioritized? Is Java thread scheduling preemptive?

Using a join( ) method means waiting for a thread to complete. In the example below, the main thread that spawned worker threads t1 and t2 will wait on the t1.join() line until t1 has finished its work, and then will do the same for t2.join().

 
    Thread t1 = new Thread(new EventThread("event-1"));
    t1.start();
    Thread t2 = new Thread(new EventThread("event-2"));
    t2.start();

    while (true) {
        try {
           t1.join();
           t2.join();    
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
     }




Q. Can you guarantee the order of thread execution in Java?
A. No. How the threads are run on depends on the Thread Scheduler. So, you cannot guarantee the order of execution. Calling start() doesn't mean run() will be called immediately, it depends on thread scheduler when it chooses to run your thread.

However, when you use join(), it makes sure that as soon as a thread calls join,the current thread will be blocked until the thread you have called join is finished.

Q. Can you call the run( ) directly instead of calling start() in Java thread?
A. No. Calling run() directly just executes the code synchronously in the same thread without spawning a new worker thread, just as a normal method call.

The start() method starts the execution of the new thread and calls the run() method. The start() method returns immediately and the new thread normally continues until the run() method returns.

Java Thread Object's constructor (e.g. new Thread) creates a Java Thread Object, but not an OS level thread - and the start() method creates an OS level thread.

So, never call run() method directly. t1.start( ) will internally call run( ).

Q. Can you restart a thread that is already started by calling the start( ) method again?
A. No. The Java API says "It is never legal to start a thread more than once". throws an IllegalThreadStateException - if the thread was already started. A thread's life-cycle completes once it completes execution.

Q. What is a daemon thread?
A. Daemon threads in Java are those threads which run in background.Background threads performing house keeping tasks. These threads continue to execute even after the thread that spawned them exits. For example,

  •  JVM spawns a low priority daemon thread to perform Garbage collection.
  • Thread.setDaemon(true) makes a thread daemon, but it can only be called before starting a thread in Java. It will throw IllegalThreadStateException if corresponding Thread is already started and running, and you try to call setDaemon(true).

Q. Are threads daemon by default when created with new Thread(...)?
A. When code running in some thread creates a new Thread object, and weather it is a daemon or user (i.e. non daemon) thread set equal to the thread that created it. The main thread in Java that is created implicitly by the JVM is a user thread (i.e. Non Daemon).



Q. How are threads prioritized?
A. Every thread has a priority. Threads with higher priority are executed in preference to threads with lower priority by the thread scheduler. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread. This is similar to how a thread is daemon or not is set. But, the daemon or not needs to be set before starting a thread, whereas you can modify a thread's priority at any time after its creation using the setPriority() method. 

You have the setPriority ( ) method in the Thread class, but how the priority works depends on the underlying native platform  like Windows, Linux, Solaries, etc. 

public static void main(String args[]) {
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    // ...................
}



Q. Is Java thread scheduling preemptive?
A. English dictionary definition of preempt means "Act in advance".

In general, there are two types of scheduling: non-preemptive scheduling, and preemptive scheduling. In non-preemptive scheduling, a thread runs until it terminates, stops, blocks, suspends, or yields. In preemptive scheduling, even if the current thread is still running, a context switch will occur when its time slice is used up.


The Java run-time system's thread scheduling algorithm is preemptive, which means if at any time a thread with a higher priority than all other "runnable" threads becomes "runnable", the run-time system chooses the new higher priority thread for execution. The new higher priority thread is said to preempt the other threads.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home