28 29 30 31 } 32 } 33 34 oddPrinted = false; 35 out.println("printEven() " + number); notifyAll(); //notify all waiting threads 36 } 37 } 38 39 40 The output will be something like 1 printOdd() 1 2 printEven() 2 3 printOdd() 3 4 printEven() 4 5 printOdd() 5 6 printEven() 6 printOdd() 7 7 printEven() 8 8 printOdd() 9 9 printEven() 10 10 Note: This a typical example of splitting tasks among threads. A method calls notify/notifyAll( ) as the last thing it does (besides return). Since the printOdd( ) and printEven( ) methods were void, the notifyAll( ) was the last statement. If it were to return some value, the notifyAll( ) would have been placed just before the return statement. Q. Write a multi-threaded Java program in which, one thread generates odd numbers and write to a pipe and the second thread generates even numbers and write to another pipe, and a third thread receives the numbers from both the pipes and evaluates if the sum is multiples of 5? A. In Unix, a pipe (“|”) operator helps you to redirect output from one command to another. PipedReader and PipedWriterclasses in java.io package helps you to do the same. It helps you to redirect the read input into writer seamlessly. In Unix, two different processes on different address spaces can communicate using pipe, but in java two threads on the JVM can communicate using Piped ByteStream/CharacterStream within the same process (i.e same address space) Here is the code snippet. The Writer threads responsible for writing odd and even numbers to the respective pipes. 1 package multithreading; 2 import java.io.IOException; 3 import java.io.PipedWriter; 4 5 public class NumberWriter extends Thread { 6 7 private PipedWriter writer;
28 29 30 31 32 33 34 35 36 37 38 39 40 } } oddPrinted = false; out.println("printEven() " + number); notifyAll(); //notify all waiting threads } } The output will be something like 1 2 3 4 5 6 7 8 9 10 printOdd() 1 printEven() 2 printOdd() 3 printEven() 4 printOdd() 5 printEven() 6 printOdd() 7 printEven() 8 printOdd() 9 printEven() 10 Note: This a typical example of splitting tasks among threads. A method calls notify/notifyAll( ) as the last thing it does (besides return). Since the printOdd( ) and printEven( ) methods were void, the notifyAll( ) was the last statement. If it were to return some value, the notifyAll( ) would have been placed just before the return statement. Q. Write a multi-threaded Java program in which, one thread generates odd numbers and write to a pipe and the second thread generates even numbers and write to another pipe, and a third thread receives the numbers from both the pipes and evaluates if the sum is multiples of 5? A. In Unix, a pipe (“|”) operator helps you to redirect output from one command to another. PipedReader and PipedWriterclasses in java.io package helps you to do the same. It helps you to redirect the read input into writer seamlessly. In Unix, two different processes on different address spaces can communicate using pipe, but in java two threads on the JVM can communicate using Piped ByteStream/CharacterStream within the same process (i.e same address space) Here is the code snippet. The Writer threads responsible for writing odd and even numbers to the respective pipes. 1 2 3 4 5 6 7 package multithreading; import java.io.IOException; import java.io.PipedWriter; public class NumberWriter extends Thread { private PipedWriter writer;