Multithreading
Multithreading
Fig. 1 Multithreading
A thread in Java can exist in any one of the following states at any given time. A thread
lies only in one of the shown states at any instant:
1. New State
2. Runnable State
3. Blocked State
4. Waiting State
5. Timed Waiting State
6. Terminated State
Fig. Life Cycle of Thread
1. New Thread: When a new thread is created, it is in the new state. The thread has not
yet started to run when the thread is in this state. When a thread lies in the new state, its
code is yet to be run and hasn’t started to execute.
2. Runnable State: A thread that is ready to run is moved to a runnable state. In this state,
a thread might actually be running or it might be ready to run at any instant of time. It is
the responsibility of the thread scheduler to give the thread, time to run. A multi-
threaded program allocates a fixed amount of time to each individual thread. Each and
every thread get a small amount of time to run. After running for a while, a thread
pauses and gives up the CPU so that other threads can run
3. Running: it means the process has given its time to the thread for its execution. After
generation of run() method the thread actually being with its execution. In this state
thread is actually being run and display the output
4. Blocked: The thread will be in blocked state when it is trying to acquire a lock but
currently the lock is acquired by the other thread. The thread will move from the
blocked state to runnable state when it acquires the lock.
4. Waiting state: The thread will be in waiting state when it calls wait() method or join()
method. It will move to the runnable state when other thread will notify or that thread
will be terminated.
5. Timed Waiting: A thread lies in a timed waiting state when it calls a method with a
time-out parameter. A thread lies in this state until the timeout is completed or until a
notification is received. For example, when a thread calls sleep or a conditional wait, it
is moved to a timed waiting state.
It represents the runnable state.It means a thread is waiting in the queue to run.
It represents the blocked state. In this state, the thread is waiting to acquire a lock.
It represents the waiting state. A thread will go to this state when it invokes the Object.wait()
method, or Thread.join() method with no timeout. A thread in the waiting state is waiting for
another thread to complete its task.
o sleep
o join with timeout
o wait with timeout
o parkUntil
o parkNanos
Thread Creation
1. We can extend the thread class, itself.
2. We can implement the runnable statement
Constructors of Thread Class
Thread(ThreadGroup group,
Allocates a new Thread object.
Runnable target)
Throws CloneNotSupportedException as a
clone()
Thread can not be meaningfully cloned
setContextClassLoader(ClassLoa
Sets the context ClassLoader for this Thread
der cl)
Methods Action Performed
For example, in a classic queuing problem where one thread is producing data, and
the other is consuming it.
To avoid polling, Java uses three methods, namely, wait(), notify(), and
notifyAll(). All these methods belong to object class as final so that all classes
have them. They must be used within a synchronized block only.
wait(): It tells the calling thread to give up the lock and go to sleep until some
other thread enters the same monitor and calls notify().
notify(): It wakes up one single thread called wait() on the same object. It
should be noted that calling notify() does not give up a lock on a resource.
notifyAll(): It wakes up all the threads called wait() on the same object.
public static int Sets the default priority for the Thread. (Priority:
NORM_PRIORITY 5)
public static int Sets the Minimum Priority for the Thread.
MIN_PRIORITY (Priority: 1)
public static int Sets the Maximum Priority for the Thread.
MAX_PRIORITY (Priority: 10)
Example:
import java.lang.*;
class Thread1 extends Thread {
// Thread - 0, 1 , 2 signify 1 , 2 , 3
// respectively
}
}
Synchronization in Java
Example :
// Java Program to demonstrate synchronization in Java
class Counter {
private int c = 0; // Shared variable