0% found this document useful (0 votes)
42 views

Thread Control and Priorites

The document discusses thread control and priorities in Java. It describes how to check if a thread is alive using isAlive() and wait for it to finish using join(). It also discusses setting thread priority and how higher priority threads are more likely to be executed by the CPU over lower priority threads.

Uploaded by

Noumya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Thread Control and Priorites

The document discusses thread control and priorities in Java. It describes how to check if a thread is alive using isAlive() and wait for it to finish using join(). It also discusses setting thread priority and how higher priority threads are more likely to be executed by the CPU over lower priority threads.

Uploaded by

Noumya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Java Programming

Thread Control and Priorities

1 © 2012 WIPRO LTD | WWW.WIPRO.COM


Thread Control Mechanism

2 © 2012 WIPRO LTD | WWW.WIPRO.COM


Control Thread Execution

• Two ways exist by which you can determine whether a


thread has finished:

• The isAlive( ) method will return true if the thread upon


which it is called is still running; else it will return false

• The join( ) method waits until the thread on which it is


called terminates.
• Syntax:
• final boolean isAlive()
• final void join() throws InterruptedException

3 © 2012 WIPRO LTD | WWW.WIPRO.COM


Concurrency in Java (Diagram)

• When each MultiThreadWorker is ‘started’, it is executed in its own


thread, independent of the main thread.
• This application has a total of 4 application threads: the main thread,
Curly thread, Larry thread and Moe thread.
Main thread terminates
after all child threads
terminated.

Application Life Time


Application Application
Starts Ends

Main Thread Thread1 Thread2 Thread3


Starts Starts Starts Starts Main Thread
Ends
Thread1 works() Thread1
Ends

Thread2 works() Thread2


main thread Ends
initiates child
Thread3
threads
Thread3 works() Ends
Assumed time taken by
each thread are
different as indicated
4
4 © 2012 WIPRO LTD | WWW.WIPRO.COM
Control Thread Execution
class DemoThread implements Runnable {
String name;
Thread thread;

DemoThread(String threadname) {
name = threadname;
thread = new Thread(this, name);
System.out.println("New Thread: " + thread);
thread.start();
}

public void run() {


try {
for (int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
System.out.println(name + "Interrupted");
}
System.out.println(name + "Exiting");
}
}
5 © 2012 WIPRO LTD | WWW.WIPRO.COM
Control Thread Execution (Contd.).
public class MultiThreadImpl {
Output:
public static void main(String args[]) { New Thread: Thread[A,5,main]
DemoThread t1 = new DemoThread("A"); New Thread: Thread[B,5,main]
DemoThread t2 = new DemoThread("B"); Child Thread: 5
New Thread: Thread[C,5,main]
DemoThread t3 = new DemoThread("C"); Thread A is alive: true
System.out.println("Thread A is alive: " + t1.thread.isAlive()); Thread B is alive: true
System.out.println("Thread B is alive: " + t2.thread.isAlive()); Thread C is alive: true
Waiting for child threads to finish
System.out.println("Thread C is alive: " + t1.thread.isAlive()); Child Thread: 5
Child Thread: 5
try { Child Thread: 4
Child Thread: 4
System.out.println("Waiting for child threads to finish");
Child Thread: 4
t1.thread.join(); Child Thread: 3
t2.thread.join(); Child Thread: 3
t3.thread.join(); Child Thread: 3
Child Thread: 2
} catch (InterruptedException e) { Child Thread: 2
System.out.println("Main thread interrupted"); Child Thread: 2
} Child Thread: 1
Child Thread: 1
Child Thread: 1
System.out.println("Thread A is alive: " + t1.thread.isAlive()); BExiting
System.out.println("Thread A is alive: " + t1.thread.isAlive()); CExiting
AExiting
System.out.println("Thread A is alive: " + t1.thread.isAlive()); Thread A is alive: false
System.out.println("Main thread exiting"); Thread A is alive: false
} Thread A is alive: false
Main thread exiting
}

6 © 2012 WIPRO LTD | WWW.WIPRO.COM


Thread Priorities

7 © 2009 Wipro Ltd - Confidential


Thread Priorities - A Thought

• When there are multiple threads running at the same time,


how the CPU decides which thread should be given more
time to execute and complete first?

8 © 2009 Wipro Ltd - Confidential


Thread Priorities

• A thread priority decides:


– The importance of a particular thread, as compared to the other threads
– when to switch from one running thread to another

• The term that is used for switching from one thread to another is
context switch

• Threads which have higher priority are usually executed in preference to


threads that have lower priority

• When the thread scheduler has to pick up from several threads that are
runnable, it will check the thread priority and will decide when a particular
has to run

• The threads that have higher-priority usually get more CPU time as
compared to lower-priority threads
9 © 2009 Wipro Ltd - Confidential
Deciding on a Context Switch

• A thread can voluntarily relinquish control by explicitly yielding,


sleeping, or blocking on pending Input/ Output
• All threads are examined and the highest-priority thread that is
ready to run is given the CPU
• A thread can be preempted by a higher priority thread
• A lower-priority thread that does not yield the processor is
superseded, or preempted by a higher-priority thread

This is called preemptive multitasking.

• When two threads with the same priority are competing for
CPU time, threads are time-sliced in round-robin fashion in case
of Windows like OSs

10 © 2009 Wipro Ltd - Confidential


Thread Preemption

• A higher priority thread can also preempt a lower priority


thread
– Actually, threads of equal priority should evenly split the CPU
time
– Every thread has a priority

• When a thread is created it inherits the priority of the thread


that created it

11 © 2009 Wipro Ltd - Confidential


Thread Priorities – accessor/mutator
• The methods for accessing and setting priority are as follows:

public final int getPriority( );


public final void setPriority (int level);

• When a new Java thread is created it has the same priority as the thread
which created it

• Thread priority can be changed by the setPriority() method

thread1.setPriority(Thread.NORM_PRIORITY + 1);
thread2.setPriority(Thread.NORM_PRIORITY -1);
thread3.setPriority(Thread.MAX_PRIORITY - 1);

thread1.start();
thread2.start();
thread3.start();
Note: Implementation of thread priority varies depending on the underlying
Operating System, and thus should never be considered as a factor when
designing application logic.
12 © 2009 Wipro Ltd - Confidential
Thread Priority - Example
To demonstrate thread priority, we will use the same demo from the ‘creating multiple threads’
topic.
Modifying the program to include thread priority:

public class ThreadDemo implements Runnable {


public void run() {
for(int counter=1;counter<=100;counter++){
System.out.println(Thread.currentThread().getName()+"thread is
running..."+counter);
}}
public static void main(String args[]) {
ThreadDemo threadDemo = new ThreadDemo(); Setting MAX
Thread t1 = new Thread(threadDemo,"First"); priority to t1
t1.setPriority(Thread.MAX_PRIORITY);
Thread t2 = new Thread(threadDemo,"Second");
t2.setPriority(Thread.MIN_PRIORITY); Setting MIN
t1.start(); priority to t2
t2.start();
}}

13 © 2009 Wipro Ltd - Confidential


Example on Thread Priority
Output:
Firstthread is running...1
Firstthread is running...2
Secondthread is running...1
Firstthread is running...3
Firstthread is running...4 We can see that the First thread is
Firstthread is running...5 given more execution time than the
Firstthread is running...6
Second thread since First thread is
Firstthread is running...7
having MAX priority.
Firstthread is running...8
Firstthread is running...9
Firstthread is running...10
Firstthread is running...11
Firstthread is running...12
Firstthread is running...13
Secondthread is running...2
Firstthread is running...14
Firstthread is running...15
Firstthread is running...16
Firstthread is running...17
Firstthread is running...18
Firstthread is running...19
14
Firstthread is running...20 © 2009 Wipro Ltd - Confidential
Assignment

15 © 2012 WIPRO LTD | WWW.WIPRO.COM


Crossword

Please use the excel


sheet to complete the
crossword

16 © 2012 WIPRO LTD | WWW.WIPRO.COM


Summary

• Different Thread control mechanisms


• Different Thread priorities
• Setting the priority of the thread
• Getting thread priority

17 © 2012 WIPRO LTD | WWW.WIPRO.COM


Thank You

18 © 2012 WIPRO LTD | WWW.WIPRO.COM

You might also like