Exercises 2: Thread 1. Thread priority Ex 1: Priority class PriThread extends Thread { PriThread(String name, int pri) { super(name); setPriority(pri); start(); } public void run() { System.out.println(getPriority()); } } public class Main { public static void main(String args[]) throws Exception { PriThread mt2 = new PriThread("Low Priority", Thread.NORM_PRIORITY - 1); PriThread mt1 = new PriThread("High Priority", Thread.NORM_PRIORITY + 1); mt1.join(); mt2.join(); } } Ex 2: Set Priority public class Main { public static void main(String[] args) throws Exception { Thread thread1 = new Thread(new TestThread(1)); Thread thread2 = new Thread(new TestThread(2)); thread1.setPriority(Thread.MAX_PRIORITY); thread2.setPriority(Thread.MIN_PRIORITY); thread1.start(); thread2.start(); thread1.join(); thread2.join(); } } class TestThread implements Runnable { int id; public TestThread(int id) { this.id = id; } public void run() { for (int i = 1; i <= 10; i++) { System.out.println("Thread" + id + ": " + i); } } } Ex 3: Join and isAlive Method public class ThreadDemo implements Runnable{ String name; Thread objTh; public ThreadDemo(String name) { this.name = name; objTh = new Thread(this, name); System.out.println("New Thread are starting "+ objTh); objTh.start(); } public void run() { try { for(int count =0; count <2; count++) { System.out.println(name + " : "+count); objTh.sleep(1000); } } catch (InterruptedException e) { System.out.println(name + " interrupted"); } System.out.println(name + " exiting"); } public static void main(String[] args) { ThreadDemo objNew1 = new ThreadDemo("Demo One"); ThreadDemo objNew2 = new ThreadDemo("Demo Two"); ThreadDemo objNew3 = new ThreadDemo("Demo Three"); System.out.println("First thread is Alive: "+objNew1.objTh.isAlive()); System.out.println("Second thread is Alive: "+objNew2.objTh.isAlive()); System.out.println("First thread is Alive: "+objNew3.objTh.isAlive()); try { System.out.println("I am in the main and waitting fot the thread to finish"); objNew1.objTh.join(); objNew2.objTh.join(); objNew3.objTh.join(); } catch (Exception e) { System.out.println("Main thread is interrupted"); } System.out.println("First thread is Alive: "+objNew1.objTh.isAlive()); System.out.println("Second thread is Alive: "+objNew2.objTh.isAlive()); System.out.println("First thread is Alive: "+objNew3.objTh.isAlive()); } } Ex 4: Synchronized 1. Synchronized Code: public class One { synchronized void display(int num) { System.out.println("" + num); try { Thread.sleep(1000); } catch (Exception e) { System.out.println("Interrupted"); } System.out.println("Done"); } public class Two implements Runnable{ int number; One objOne; Thread objTh; public Two(One objOne, int num) { this.objOne= objOne; this.number = num; objTh = new Thread(this); objTh.start(); } public void run() { objOne.display(number); } } public class SynchMethod { public static void main(String[] args) { One objOne = new One(); int digit = 10; Two objSynch1 = new Two(objOne, digit++); Two objSynch2 = new Two(objOne, digit++); Two objSynch3 = new Two(objOne, digit++); try { objSynch1.objTh.join(); objSynch2.objTh.join(); objSynch3.objTh.join(); } catch (Exception e) { System.out.println("Interrupted"); } } } 2. Synchronized Block: public class DemoOne { void display(int num) { System.out.println("" + num); try { Thread.sleep(1000); } catch (Exception e) { System.out.println("Interrupted"); } System.out.println("Done"); } } public class DemoTwo implements Runnable{ int number; DemoOne objOne; Thread objTh; public DemoTwo(DemoOne objOne, int num) { this.objOne= objOne; this.number = num; objTh = new Thread(this); objTh.start(); } public void run() { synchronized(objOne) { objOne.display(number); } } } public class SynchBlock { public static void main(String[] args) { DemoOne objOne = new DemoOne(); int digit = 10; DemoTwo objSynch1 = new DemoTwo(objOne, digit++); DemoTwo objSynch2 = new DemoTwo(objOne, digit++); DemoTwo objSynch3 = new DemoTwo(objOne, digit++); try { objSynch1.objTh.join(); objSynch2.objTh.join(); objSynch3.objTh.join(); } catch (Exception e) { System.out.println("Interrupted"); } } } } . Thread( new TestThread (2) ); thread1 .setPriority (Thread. MAX_PRIORITY); thread2 .setPriority (Thread. MIN_PRIORITY); thread1 .start(); thread2 .start(); thread1 .join(); thread2 .join(); }. Ex 2: Set Priority public class Main { public static void main(String[] args) throws Exception { Thread thread1 = new Thread( new TestThread(1)); Thread thread2 = new Thread( new TestThread (2) );. PriThread mt2 = new PriThread("Low Priority", Thread. NORM_PRIORITY - 1); PriThread mt1 = new PriThread("High Priority", Thread. NORM_PRIORITY + 1); mt1.join(); mt2.join();