There are two ways to define and run threads in Java programming language.
First way to define threads is to extend from java.lang.Thread class, and override its run() method. You instantiate and start this thread by creating a new instance of your class and calling the start() method
Second way to define threads is to implement java.lang.Runnable interface, and implement its run method. You can instantiate and start this thread by creating a creating a new instance of your class that implements Runnable interface, and passing this instance of Runnable to a new Instance of Thread.
Defining a thread by implementing the Runnable is the preferred way, since if you make your class extend from the Thread class, you will not be able to extend from any other class.
/** Extending from java.lang.Thread */
//Define
Class MyThread extends Thread {
public void run() {...}
}
//Instantiate
MyThread t = new MyThread();
//Start
t.start();
/** Implementing java.lang.Runnable interface */
//Define
Class MyRunnable implements Runnable {
public void run() {...}
}
//Instantiate
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
//Start
t.start()
A thread can be in one of five states
New - A thread is in 'New' state when it has been instantiated, but the start() method is not yet called on the thread instance. In this state the thread is a Thread object, but it is not yet a thread of execution.
Runnable - A thread is in Runnable state when the start() method has been called on the thread instance but it is not yet picked up by the thread scheduler for execution.
Running - A thread is in Running state when it is currently executing . The thread scheduler picks up the thread from the thread pool and executes it.
Waiting, Blocked, Sleeping - In this state the thread is not eligible for execution and is not in the running pool. A thread may be ineligible for execution for a variety of reasons. The thread may be blocked waiting for an IO resource or on a lock. The thread may be sleeping since the code tells it to sleep for a specific period of time. Or the thread may be waiting on another thread to send it a notification.
Dead - A thread is in dead state when the execution of its run() method is complete. Once a thread is dead it cannot be run again, hence a thread cannot transition from dead state to any other state.
You can pause the execution of a thread for a certain period of time by calling its sleep() method and passing the time that the thread has to stop executing.
Though you specify the time period that the thread should sleep for, there is no guarantee that the thread will pause executing exactly for that period of time.
The thread may start executing sooner than the specified time - if it gets an interrupt from another thread. Or the thread may start executing later than the specified time - After the specified sleep period of time has elapsed, the thread is put into the runnable pool of threads from where it will be picked up by the scheduler. The scheduler may pick up this thread for execution immediately or if there are higher priority threads then it will be executed later.
The join() method allows one thread to wait for the completion of another thread. For example - If t is a Thread instance, then calling t.join() in the current thread, say the main thread, will cause the main thread to wait until the t thread has completed its execution.
Sleep() method is used to delay the execution of the thread for a specific period of time...
*** See complete answer in the Java Interview Guide.
Yeild() method stops a running thread so that other threads which are in the waiting state and are of the same priority as the running thread will get a chance to execute...
*** See complete answer in the Java Interview Guide.
Java programming language provides the 'synchronized' keyword which allows only one thread at a time to access the shared resource...
*** See complete answer in the Java Interview Guide.
Synchronization is based on internal entities called intrinsic locks or monitor locks. Every object has an intrinsic lock associated with it...
*** See complete answer in the Java Interview Guide.
A thread cannot acquire a lock that is held by another thread. But a thread can acquire a lock that it already owns...
*** See complete answer in the Java Interview Guide.
Java programming language provides the wait(), notify() and notifyAll() methods that facilitate thread to thread communication.
wait() method lets a running thread, say Thread A, stop execution and wait on a specific action to complete in another thread, say Thread B. Thread A releases its locks and goes into waiting state while waiting for the specific action to complete on Thread B...
*** See complete answer in the Java Interview Guide.