Docsity

Java Multithreading: Creating and Managing Threads, Lecture notes of Java Programming

Partial preview of the text

Download Java Multithreading: Creating and Managing Threads and more Lecture notes Java Programming in PDF only on Docsity!

Multithreading

Lec 23

docsity.com

Threads

 The ability to do multiple things at once within the same

application

 Finer granularity of concurrency

 Lightweight

 Easy to create and destroy

 Shared address space

 Can share memory variables directly

 May require more complex synchronization logic because of

shared address space

docsity.com

Example Code: ThreeLoopTest

public class ThreeLoopTest { public static void main (String args[ ]){

// first loop for (int i=1; i<= 5; i++) System.out.println(“first ” +i);

// second loop for (int j=1; j<= 5; j++) System.out.println(“second ” + j);

// third loop for (int k=1; k<= 5; k++) System.out.println(“third ” + k);

} }

docsity.com

Compile & Execute: ThreeLoopTest

docsity.com

Creating Threads in Java

 Two approaches

 Using Interface

 Implement the runnable interface in a class  Provide an implementation for the run() method  Instantiate Thread object by passing runnable object in constructor  Start thread

 Using Inheritance

 Subclass java.lang.Thread  Override the run() method  Instantiate Subclass Thread Object  Start thread

docsity.com

Thread Creation Steps : using Interface

Step 1 - Implement the Runnable Interface

class Worker implements Runnable

Step 2 - Provide an Implementation of run method

public void run ( ){ // write thread behavior // code that will execute by thread }

Step 3 - Instantiate Thread object by passing runnable object in constructor

Worker w = new Worker(“first”); Thread t = new Thread (w);

Step 4 – Start thread t.start()

docsity.com

Three Loops

Multi-Threaded Execution

docsity.com

Example Code: using Interface

public class Worker implements Runnable {

private String job ;

public Worker (String j ){

job = j;

public void run ( ) {

for(int i=1; i<= 10; i++)

System.out.println(job + " = " + i);

docsity.com

Thread Priorities

 Every Thread has a priority

 Threads with higher priority are executed in preference

to threads with lower priority

 A thread’s default priority is same as that of the creating

thread

docsity.com

Thread Priorities

 You can change thread priority by using any of the 3

predefined constants

 Thread.MAX_PRIORITY (typically 10)

 Thread.NORM_PRIORITY (typically 5)

 Thread.MIN_PRIORITY (typically 1)

 OR any integer value from 1 to 10 can be used as thread

priority.

docsity.com

Thread priority scheduling example

Priority 9 Priority 8 Priority 7

Priority 10

Priority 6 Priority 5 Priority 4 Priority 3 Priority 2 Priority 1

A B

D

C

E F G H I

J K

Ready threads

Thread.MIN_PRIORITY

Thread.MAX_PRIORITY

Thread.NORM_PRIORITY

Credit: Advanced Java, Dietel & Dietel docsity.com

Code Example: PriorityEx.java

public class PriorityEx{

public static void main (String args[ ]){

Worker first = new Worker (“first job”); Worker second = new Worker (“second job”);

Thread t1 = new Thread (first ); Thread t2 = new Thread (second);

t1.setPriority(Thread.MIN_PRIORITY); t2.setPriority(Thread. MAX_PRIORITY);

t1.start(); t2.start(); } }

docsity.com

Thread Priorities

 Problems

 A Java thread priority may map differently to the thread

priorities of the underlying OS

 Solaris has 2^32 – 1 priority levels;  Windows NT has only 7 user priority levels

 Starvation can occur for lower-priority threads if the higher-

priority threads never terminate, sleep, or wait for I/O

docsity.com