1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 package deadlock; public DeadlockTest extends Thread { public static Object lock1 = new Object(); public static Object lock2 = new Object(); public void method1() { synchronized (lock1) { delay(500); //some operation System.out.println("method1: " + Thread.currentThread().getName()); synchronized (lock2) { System.out.println("method1 is executing .... "); } } } public void method2() { synchronized { delay(500); //some operation System.out.println("method1: " + Thread.currentThread().getName()); synchronized (lock1) { System.out.println("method2 is executing .... "); } } } @Override public void run() { method1(); method2(); } public static void main(String[] args) { DeadlockTest thread1 = new DeadlockTest(); DeadlockTest thread2 = new DeadlockTest(); } /** * The delay is to simulate some real operation happening. * @param timeInMillis */ private void delay(long timeInMillis) { try { } (InterruptedException e) { e.printStackTrace(); } } }
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 } public static void main(String[] args) { NumberUtility numUtility = new NumberUtility(); //single instance shared by oddGen final int MAX_NUM = 10; //create 2 threads, one to generate odd numbers and the other to generate even NumberGenerator oddGen = new NumberGenerator(numUtility, MAX_NUM, false); NumberGenerator evenGen = new NumberGenerator(numUtility, MAX_NUM, true); //start the thread - invokes the run() method on NumberGenerator evenGen.start(); //start the thread - invokes the run() method on NumberGenerator } } Next, create the utility that is used for communicating between the two threads with wait() and notifyAll() methods via synchronized methods. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package multithreading; import static java.lang.System.out; public class NumberUtility { boolean oddPrinted = false; public synchronized void printOdd(int number) { (oddPrinted == true) { try { wait(); // waits until notified by even thread } catch (InterruptedException e) { e.printStackTrace(); } } out.println("printOdd() " + number); oddPrinted = true; notifyAll(); //notify all waiting threads } public synchronized void printEven(int number) { while (oddPrinted == false) { try { wait(); //waits until notified by the odd thread } catch (InterruptedException e) {
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 private int maxNumber; private boolean isEvenNumber; public NumberWriter(PipedWriter writer, int maxNumber, boolean isEvenNumber) { this.writer = writer; this.maxNumber = maxNumber; this.isEvenNumber = isEvenNumber; } public void run() { int i = 1; while (i <= maxNumber) { try { if (isEvenNumber && (i % 2) == 0) { writer.write(i); } else if (!isEvenNumber && i%2 != 0) { writer.write(i); } ++i; } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { final int MAX_NUM = 10; PipedWriter oddNumberWriter = new PipedWriter(); PipedWriter evenNumberWriter = new PipedWriter(); NumberWriter oddGen = new NumberWriter(oddNumberWriter, MAX_NUM, false); NumberWriter evenGen = new NumberWriter(evenNumberWriter, MAX_NUM, true); NumberReceiver receiver = new NumberReceiver(oddNumberWriter, evenNumberWriter); evenGen.start(); receiver.start(); } } The receiver thread that listens to both odd and even number pipes and computes the sum. If the sum is a multiple of 5, it prints the numbers and the sum. 1 2 3 4 package multithreading; import java.io.IOException; import java.io.PipedReader; import java.io.PipedWriter;