Upload
myrajendra
View
155
Download
0
Tags:
Embed Size (px)
Citation preview
Thread Lifecycle and Thread Methods
1
2
Objective
On completion of this period, you would be able to learn
• Thread Lifecycle• Thread methods• Example Programs
Recap
• In the previous class we have seen how to set the priorities of threads
• Thread class provides 3 constants for setting priorities• Thread.NORM_PRIORITY• Thread.MAX_PRIORITY• Thread.MIN_PRIORITY
• Thread class also provides two methods• setPriority()• getPriority()
3
Thread Life Cycle
• Just like a Process has life cycle• Thread also has a life cycle• A Thread under goes different states while it is
executing
• We know the process state diagram• It is used to represent the life cycle of a process• Similarly the life cycle of a thread can be represented by
thread state diagram
4
Thread Life Cycle
• A Thread can be in one of the following states• New born • Running• Ready to run• Blocked• Dead
5
Contd . . .
Thread Life Cycle
6
New bornNew born
Running Ready to Run
BlockedBlocked
DeadDead
stop()
stop()
stop()
start()
suspend()wait()
resume()notifyAll()
sleep() notify()
yield()
Fig. 36.1. Thread lifecycle – state transition diagram
The following figure shows the life cycle of a thread
Contd . . .
Thread Life Cycle
• When a thread object is created, it enters new born state• start() method changes the state to either running or
ready to state• suspend(), sleep()or wait() methods moves the thread to
a blocked state• resume(), notify() or notifyAll() methods moves the
blocked thread to running/ready to run state• A call to stop() method kills the thread• A running thread can give away the control of execution
to another thread by calling yield() methods
7
Contd . . .
8
Methods of Thread
•Some of the methods of the Thread class are • start()• stop()• suspend()• resume()• wait()• notify()• notifyAll()• isAlive()• yield()• join()
isAlive() method
• The signature of this method
• Returns true if the thread is not dead (run has not completed)
• Returns false if the thread is dead
9
public final boolean isAlive()
join() method
• The signature of join() method
• Calling thread waits for thread receiving message to die before it can proceed
• No argument or 0 millisecond argument means thread will wait indefinitely
• Can lead to deadlock/indefinite postponement
10
public final void join();public final void join(long t);
Example Program
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
11
Example Program
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
} 12
Contd . . .
Example Program
class DemoJoin {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
NewThread ob3 = new NewThread("Three");
System.out.println("Thread One is alive: “ + ob1.t.isAlive());
System.out.println("Thread Two is alive: “ + ob2.t.isAlive());
System.out.println("Thread Three is alive: “ + ob3.t.isAlive());
13
Use of isAlive()At this moment all threads are alive and return true
Contd . . .
Example Program
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
ob3.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Thread One is alive: “ + ob1.t.isAlive());
System.out.println("Thread Two is alive: “ + ob2.t.isAlive());
System.out.println("Thread Three is alive: “ + ob3.t.isAlive());
System.out.println("Main thread exiting.");
}
} 14
main() thread waits until all threads finish their work
As threads have finished, isAlive() returns false
Contd . . .
Example Program
15
Output
Contd . . .
suspend() method
• The signature of this method
• If the thread is alive, it is suspended and makes no further progress unless and until it is resumed
16
public final void suspend();
resume() method
• The signature of this method
• If the thread is alive but suspended, it is resumed and is permitted to make progress in its execution.
17
public final void resume();
Example Program
class NewThread implements Runnable {
string name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
18
Example Program
public void run() {
try {
for(int i = 10; i > 0; i--) {
System.out.println(name + ": " + i);
Thread.sleep(200);
}
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
}
System.out.println(name + " exiting.");
}
}
19
Contd . . .
Example Program
class SuspendResume {
public static void main(String args[]) {
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("Two");
try {
Thread.sleep(1000);
ob1.t.suspend();
System.out.println("Suspending thread One");
Thread.sleep(1000);
ob1.t.resume();
System.out.println("Resuming thread One");
ob2.t.suspend();
System.out.println("Suspending thread Two"); 20
Contd . . .
Example Program
Thread.sleep(1000);
ob2.t.resume();
System.out.println("Resuming thread Two");
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
21
Contd . . .
Example Program
// wait for threads to finish
try {
System.out.println("Waiting for threads to finish.");
ob1.t.join();
ob2.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
} 22
Contd . . .
Example Program
23
Output Contd . . .
Summary
• In this class we have discussed• The life cycle of thread• The state transition diagram• The thread methods
• isAlive()• join()• suspend()• resume()
• In the next lesson we look at the concept of synchronization
24
Quiz
1. A suspended thread goes to ready state by calling
A. start() method
B. ready() method
C. resume() method
D. None
25
2. Which of the following method changes the state of the thread to blocked state ?
A. stop()
B. suspend()
C. notify()
D. notify All()
26
Quiz Contd..
Quiz Contd..
3. What is the return type of isAlive() method ?
A. int
B. float
C. double
D. boolean
27
4. Which of the following method kills the thread ?
A. kill()B. stop()C. end()D. None
28
Quiz Contd..
Frequently Asked Questions
1. Draw and explain the state-transition diagram of thread
2. What are the different states the thread enters while it is executing ?
3. Write the syntax of using isAlive(), join() methods
4. Write the syntax of using suspend(), resume() methods
29