Multithreading
Multithreading
New − A new thread begins its life cycle in the new state. It remains in this
state until the program starts the thread. It is also referred to as a born
thread.
Runnable − After a newly born thread is started, the thread becomes
runnable. A thread in this state is considered to be executing its task.
Waiting − Sometimes, a thread transitions to the waiting state while the
thread waits for another thread to perform a task. A thread transitions back
to the runnable state only when another thread signals the waiting thread to
continue executing.
Timed Waiting − A runnable thread can enter the timed waiting state for a
specified interval of time. A thread in this state transition back to the
runnable state when that time interval expires or when the event it is waiting
for occurs.
Terminated (Dead) − A runnable thread enters the terminated state when it
completes its task or otherwise terminates.
Thread Priorities
Every Java thread has a priority that helps the operating system determine the order
in which threads are scheduled.
Java thread priorities are in the range between MIN_PRIORITY (a constant of 1) and
MAX_PRIORITY (a constant of 10). By default, every thread is given priority
NORM_PRIORITY (a constant of 5).
Step 1
Step 2
Step 3
Once a Thread object is created, it can be started by calling start() method, which
executes a call to run( ) method.
void start();
private Thread t;
private String threadName;
Step 1
The run( ) method available in Thread class needs to be overriden. This method
provides an entry point for the thread and the complete business logic is to be put
inside this method.
public void run( )
Step 2
Once Thread object is created, it can be started by calling start() method, which
executes a call to run( ) method.
void start( );
class ThreadDemo extends Thread {
private Thread t;
private String threadName;