(AJP) Chapter 3
(AJP) Chapter 3
(AJP) Chapter 3
INFORMATICS
DEPARTMENT OF SOFTWARE
ENGINEERING
BY SAMSON R.
Chapter three
Multithreading
Contents
Introduction.
Threads in Java.
Threads life cycle.
Threads Priority.
Threads synchronization.
Deadlocks.
Introduction
RUNNING.
It means that the processor has given its time to the thread for execution.
A thread keeps running until the following condition occurs.
A. Thread give up its control by its own.
i. get suspended by suspend() method which can only be revived by
resume() method.
ii. A thread is made to sleep for a specified period of time using
sleep(time) method, where time is in milliseconds.
iii. A thread is made to wait for some event to occur using wait() method.
In this case a thread can be scheduled to run again using notify() method.
DEAD
A thread that has exited is in this state.
Cont..
Cont..
Cont..
When the object of a user Thread class is created, the thread moves to
the NEW state.
After invocation of the start() method, the thread shifts from the NEW
to the ready (RUNNABLE) state and is then dispatched to running
state by the JVM thread scheduler.
After gaining a chance to execute, the run() method will be invoked.
It should be noted that when the run() method is invoked directly
(explicitly in the program), the code in it is executed by the current
thread and not by the new thread (as the thread object is not assigned
to the virtual machine scheduler).
Cont..
Depending on program operations or invocation of methods such as wait(),
sleep(), and suspend()or an I/O operation, the thread moves to the
WAITING, SLEEPING, and BLOCKED states respectively.
It should be noted that the thread is still alive.
After the completion of an operation that blocked it (I/O or sleep) or
receiving an external signal that wakes it up, the thread moves to ready state.
Then the JVM thread scheduler moves it to running state in order to
continue the execution of remaining operations.
When the thread completes its execution, it will be moved to
TERMINATED state.
A dead thread can never enter any other state, not even if the start() method
is invoked on it.
Sample code
class thread2 extends Thread{ class test {
public static void main(String[] args)
public void run()
{
{
System.out.println("thread 2 is thread2 th=new thread2();
running..."); thread3 th2=new thread3();
}}
class thread3 extends Thread{
th.start();
public void run()
th2.start();
{
System.out.println("thread 3 is
running..."); }}
}}
Cont..
A close observation of the output of two different runs reveals
that the execution/completion of threads does not need to be in
the order of their start.
Because of various operations that are performed within JVM,
threads, and different applications running at different times, it
is hard to predict the precise time at which context-switching
occurs.
It is possible to enforce the order of execution to some extent
by setting different priorities for threads.
THREAD PRIORITY
In Java, all the thread instances the developer created have the
same priority, which the process will schedule fairly without
worrying about the order.
It is possible to control the priority of the threads by using the
Java APIs.
The Thread.setPriority(…)method serves this purpose.
The Thread class provides 3 constants value for the priority: