File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Synchronizing thread woes! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Synchronizing thread woes!" Watch "Synchronizing thread woes!" New topic

Synchronizing thread woes!

Rogelio Tristan

Joined: Nov 16, 2004
Posts: 4
heres my code (runs and compiles fine!)

okay I figured out how to make a thread, and it puts numbers in a Queue, and then the consumer consumes the Queue. but my biggest problem is I need to have 2 consumers, one consumes 5, then the other consumes 5. how is that possible? I have thrown various counters in there but am still having quite some trouble, I'm not asking for just the answer, I'm asking for an explanation as to why the answer would be that. What do I do to make it comsume 5 first, then 5 to the next? any help is appreciated guys
Henry Wong

Joined: Sep 28, 2004
Posts: 20538

Step 1: Don't consuming anything until there is at least 5 items.



Step 2: Consume 5 items without releasing the lock. I'll let you figure that out yourself.

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rogelio Tristan

Joined: Nov 16, 2004
Posts: 4
okay I appreciate the help and letting me try to figure it out myself.. let me know if I am hot or cold okay.. I was thinking putting the;

statement 5 times; when I did it did it 5 times, then passed it back to the producer, then it went into this endless loop. so thats not it.

this has to do with the insertion of a;
statement am i correct? perhaps get it to wait for 5 threads, then use notify to wake up the other thread again... hmm...
I agree. Here's the link:
subject: Synchronizing thread woes!
It's not a secret anymore!