Multi-threading with blocking queues
Q. How would you go about implementing a scenario where two individual threads generate odd and even numbers respectively, and a third thread that prints the sum of odd and even number that is a multiple of 5
A. This can be achieved with multi-threading and a BlockingQueue. Earlier we saw this with piped reader/writer. A queue is a First-In-First-Out (FIFO) data structure.
A blocking queue has the following behavior
- A thread trying to take from an empty queue is blocked until some other thread puts an item into the queue.
- A thread trying to put an item in a full queue is blocked until some other thread makes space in the queue, either by taking one or more items or completely clearing the queue.
The odd and even number generator classes
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class NumberWriter extends Thread { private BlockingQueue<Integer> queue; private int maxNumber; private boolean isEvenNumber; public NumberWriter(BlockingQueue<Integer> queue, int maxNumber, boolean isEvenNumber) { this.queue = queue; this.maxNumber = maxNumber; this.isEvenNumber = isEvenNumber; } public void run() { int i = 1; while (i <= maxNumber) { try { if (isEvenNumber && (i % 2) == 0) { queue.put(i); // enqueue } else if (!isEvenNumber && i%2 != 0) { queue.put(i); } ++i; } catch (InterruptedException ie) { ie.printStackTrace(); } } } public static void main(String[] args) { final int MAX_NUM = 100; BlockingQueue<Integer> oddNumberQueue = new ArrayBlockingQueue<Integer>(10); BlockingQueue<Integer> evenNumberQueue = new ArrayBlockingQueue<Integer>(10); NumberWriter oddGen = new NumberWriter(oddNumberQueue, MAX_NUM, false); NumberWriter evenGen = new NumberWriter(evenNumberQueue, MAX_NUM, true); NumberReceiver receiver = new NumberReceiver(oddNumberQueue, evenNumberQueue); oddGen.start(); evenGen.start(); receiver.start(); } }
The thread that sums up the odd and even number, and prints it if the sum is a multiple of 5.
import java.util.concurrent.BlockingQueue; public class NumberReceiver extends Thread { private BlockingQueue<Integer> oddNumberQueue; private BlockingQueue<Integer> evenNumberQueue; public NumberReceiver(BlockingQueue<Integer> oddNumberQueue, BlockingQueue<Integer> evenNumberQueue) { this.oddNumberQueue = oddNumberQueue; this.evenNumberQueue = evenNumberQueue; } public void run() { int odd = 0, even = 0; try { while (odd != -1) { odd = oddNumberQueue.take(); //dequeue - FIFO even = evenNumberQueue.take(); //dequeue - FIFO if ((odd + even) % 5 == 0) { System.out.println("match found " + odd + " + " + even + " = " + (odd + even)); } } } catch (InterruptedException ie) { ie.printStackTrace(); System.exit(1); } } }
The ouput is
match found 7 + 8 = 15 match found 17 + 18 = 35 match found 27 + 28 = 55 match found 37 + 38 = 75 match found 47 + 48 = 95 match found 57 + 58 = 115 match found 67 + 68 = 135 match found 77 + 78 = 155 match found 87 + 88 = 175 match found 97 + 98 = 195
Labels: Multi-threading
2 Comments:
Very nice examples, thanks allot.
Very helpful examples indeed. Thanks!
Post a Comment
Subscribe to Post Comments [Atom]
<< Home