Professional Documents
Culture Documents
MULTITHREADED PROGRAMMING
MULTITHREADED PROGRAMMING
THREAD: INTRODUCTION
2. Runnable State:
⦿ The Runnable state means that the thread is ready for execution and is
waiting for the availability of the processor.
⦿ That is, the thread has joined the queue of threads that are waiting for
execution.
LIFE CYCLE OF A THREAD
3. Running State:
⦿ Running means that the processor has given its time to the
thread for its execution.
⦿ The thread runs until it relinquishes (give up) control on its own.
⦿ It has been suspended using suspend( ) method.
⦿ A suspended thread can be revived by using the resume( )
method.
⦿ we can put a thread to sleep for a specified time period using the
method sleep (time) where time is in milliseconds.
⦿ This means that the thread is out of the queue during this time
period.
⦿ It has been told to wait until some events occur.
⦿ This is done using the wait ( ) method.
⦿ The thread can be scheduled to run again using the notify ( )
method.
LIFE CYCLE OF A THREAD
4. Blocked State:
⦿ A thread is set to be blocked when it is prevented from entering into
the Runnable state and subsequently the running state.
⦿ This happens when the thread is suspended, or sleeping, or waiting in
order to satisfy certain requirements.
5. Dead State:
⦿ Every thread has a life cycle.
⦿ A running thread ends its life when it has completed executing its run
( ) method.
⦿ It is a natural death.
}
}
THREAD METHODS
⦿ Thread class methods will be used to control the behavior of
a thread.
⦿ In the given examples 1 and 2 yield() is used.
⦿ A yield() method is a static method of Thread class and it
can stop the currently executing thread and will give a
chance to other waiting threads to run. i.e., By using
yield() if currently running thread is not doing anything
particularly important and if any other threads or processes
need to be run, they should run. Otherwise, the current
thread will continue to run.
EXAMPLE 1
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 5; ++i) {
Thread.yield(); // By calling this method, MyThread stop its execution and giving a chance to a main thread
System.out.println("Thread started:" + Thread.currentThread().getName());
}
System.out.println("Thread ended:" + Thread.currentThread().getName());
}
}
public class YieldMethodTest {
public static void main(String[] args) {
MyThread thread = new MyThread();
thread.start();
for (int i = 0; i < 5; ++i) {
System.out.println("Thread started:" + Thread.currentThread().getName());
}
System.out.println("Thread ended:" + Thread.currentThread().getName());
}
}
EXAMPLE 2
class A extends Thread{
public void run() {
for(int i=1;i<=5;i++) {
if(i==1)
yield();
System.out.println("from thread A:i="+i); }
System.out.println("Exiting from A");
}
}
class B extends Thread{
public void run() {
for(int j=1;j<=5;j++) {
System.out.println("from thread B:j="+j);
if(j==3)
stop();
}
System.out.println("Exiting from B");
}
}
public class ThreadEx{
public static void main(String args[]) {
A a1=new A(); //creating threads
B b1=new B();
System.out.println("Start thread A");
a1.start();
System.out.println("Start thread B");
b1.start();
}
}
THREAD EXCEPTIONS
class MyThread extends Thread{
public void run()
{
System.out.println("Throwing in " +"MyThread");
throw new RuntimeException();
}
}
public class Main {
public static void main(String[] args)
{
MyThread t = new MyThread();
t.start();
try {
Thread.sleep(1000);
} catch (Exception x) { System.out.println("Caught it" + x);
}
System.out.println("Exiting main");
}
}
THREAD PRIORITY
⦿ In java,each thread is assigned with a priority which affects the order of their
execution.
⦿ Threads with same priority will be treated equally by java scheduler and they share
processor on FIFO basis.
Syntax: ThreadName.setPriority(intNumber);
1-min.priority
5-normal priority
10-max.priority
MIN_PRIORITY=1
NORM_PRIORITY=5
MAX_PRIORITY=10
EXAMPLE:1
class TestPriority extends Thread{
public void run()
{
System.out.println("running thread name is:"+Thread.currentThread().getName());
System.out.println("running thread priority is:"+Thread.currentThread().getPriority());
}
public static void main(String args[]){
TestPriority m1=new TestPriority();
TestPriority m2=new TestPriority();
m1.setPriority(Thread.MIN_PRIORITY);
m2.setPriority(Thread.MAX_PRIORITY);
m1.start();
m2.start();
}
}
EXAMPLE 2
public class PriorEx extends Thread{
public void run() {
System.out.println("Thread is Running..");
}
public static void main(String args[]) {
PriorEx p1=new PriorEx();
PriorEx p2=new PriorEx();
PriorEx p3=new PriorEx();
p1.start();
p2.start();
p1.setPriority(2);
p2.setPriority(6);
p3.setPriority(9);
System.out.println("p1 thread priority"+p1.getPriority());
System.out.println("p2 thread priority"+p2.getPriority());
System.out.println("p3 thread priority"+p3.getPriority());
System.out.println("AFTER CHANGING PRIORITY");
System.out.println("p1 thread priority"+"\t"+p1.MAX_PRIORITY);
System.out.println("p2 thread priority"+"\t"+p2.MIN_PRIORITY);
System.out.println("p3 thread priority"+"\t"+p3.NORM_PRIORITY);
}
}
OUTPUT:
Thread is Running..
Thread is Running..
p1 thread priority 2
p2 thread priority 6
p3 thread priority 9
AFTER CHANGING PRIORITY
p1 thread priority 10
p2 thread priority 1
p3 thread priority 5
SYNCHRONIZATION
⦿ When multiple threads are competing for same resources then they may lead to
serious problems.For Example,if thread t1 is trying to read a record from file
while another thread t2 is writing the same file then thread t1 may read wrong
data.This problem can be eliminated by using synchronization technique in
java.