29
Thread Lifecycle and Thread Methods 1

Threadlifecycle.36

Embed Size (px)

Citation preview

Page 1: Threadlifecycle.36

Thread Lifecycle and Thread Methods

1

Page 2: Threadlifecycle.36

2

Objective

On completion of this period, you would be able to learn

• Thread Lifecycle• Thread methods• Example Programs

Page 3: Threadlifecycle.36

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

Page 4: Threadlifecycle.36

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

Page 5: Threadlifecycle.36

Thread Life Cycle

• A Thread can be in one of the following states• New born • Running• Ready to run• Blocked• Dead

5

Contd . . .

Page 6: Threadlifecycle.36

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 . . .

Page 7: Threadlifecycle.36

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 . . .

Page 8: Threadlifecycle.36

8

Methods of Thread

•Some of the methods of the Thread class are • start()• stop()• suspend()• resume()• wait()• notify()• notifyAll()• isAlive()• yield()• join()

Page 9: Threadlifecycle.36

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()

Page 10: Threadlifecycle.36

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);

Page 11: Threadlifecycle.36

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

Page 12: Threadlifecycle.36

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 . . .

Page 13: Threadlifecycle.36

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 . . .

Page 14: Threadlifecycle.36

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 . . .

Page 15: Threadlifecycle.36

Example Program

15

Output

Contd . . .

Page 16: Threadlifecycle.36

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();

Page 17: Threadlifecycle.36

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();

Page 18: Threadlifecycle.36

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

Page 19: Threadlifecycle.36

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 . . .

Page 20: Threadlifecycle.36

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 . . .

Page 21: Threadlifecycle.36

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 . . .

Page 22: Threadlifecycle.36

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 . . .

Page 23: Threadlifecycle.36

Example Program

23

Output Contd . . .

Page 24: Threadlifecycle.36

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

Page 25: Threadlifecycle.36

Quiz

1. A suspended thread goes to ready state by calling

A. start() method

B. ready() method

C. resume() method

D. None

25

Page 26: Threadlifecycle.36

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..

Page 27: Threadlifecycle.36

Quiz Contd..

3. What is the return type of isAlive() method ?

A. int

B. float

C. double

D. boolean

27

Page 28: Threadlifecycle.36

4. Which of the following method kills the thread ?

A. kill()B. stop()C. end()D. None

28

Quiz Contd..

Page 29: Threadlifecycle.36

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