











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












2
4
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); } }
5
7
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
Subclass java.lang.Thread Override the run() method Instantiate Subclass Thread Object Start thread
8 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()
Three Loops Multi-Threaded Execution
11
13
14
Thread.MAX_PRIORITY (typically 10) Thread.NORM_PRIORITY (typically 5) Thread.MIN_PRIORITY (typically 1)
16 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
17
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(); } }
19
Solaris has 2 32 –1 priority levels; Windows NT has only 7 user priority levels