Monday, April 1, 2013

Passing arguments to parallelized threads in java

Java proposes a very useful class to feed threads: the BlockingQueue.

Example of declaration:
private BlockingQueue<String> _blockingQueue = new ArrayBlockingQueue<String>(3000000);
 BlockingQueue is an interface, ArrayBlockingQueue is initialized with a fixed capacity (here 3,000,000 strings).

The BlockingQueue is thread safe.

Each thread can get element with the method take().

There is no indicator that the queue is empty (as new element can be added while the queue is read). A way to send a termination signal is to use a "poisonous element" at the end of the queue. Better, is to send an interrupt signal  to all thread.

That's all folks!

No comments:

Post a Comment