Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

notifyAll and wait methods  RSS feed

 
Ugender Rekulampally
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I have a reader thread and multiple workers threads. reader thread reads data from a input source and places in the queues. and workers threads process that data from those queues.

my queues are Q1, Q2, and Q3.
Worker threads W1, W2, and W3.

W1 gets data from Q1 and W2 from Q2 ...

Reader reads data using a input source and places the data on Q1, Q2 and Q3 based on certain criteria.

my reader has this code

if any queue is not empty
then wait();
else
read the data till certain no.of elements from the input source are done.
and notifyAll();



and writers
while(Q.size() == then wait();
else
process the data.and notifyAll()


So in this situation, all of my worker threads are going to call notifyAll(). So my Reader thread is going to check for the wait condition after every thread finishes processing and makes notifyAll()..

Reader thread is going to check for the above condition
if any queue is not empty
then wait();


and goes to wait mode till all the worker threads finishes/empties the data on all three queues.


Is this design looks OK? my only concern here is executing the wait block in the reader thread after every worker thread invokes notifyAll()...


Thanks,
Ugender
 
Santhosh Kumar
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you writing this program to learn the concepts, is fine but if you want to include this type of logic in a production application, suggest to use some of the available libraries.

if you are using Java 1.5, you can use java.util.concurrent
Or if you are using <1.5, you can use backport util.concurrent

http://g.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!