Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronizing thread woes!

 
Rogelio Tristan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Step 1: Don't consuming anything until there is at least 5 items.

Change:


To:


Step 2: Consume 5 items without releasing the lock. I'll let you figure that out yourself.
 
Rogelio Tristan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Queue.store.wait();

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
Queue.store.notify();
statement am i correct? perhaps get it to wait for 5 threads, then use notify to wake up the other thread again... hmm...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic