• Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads again

 
Dimple Patra
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Suppose there are 2 threads. Thread T1 and Thread T2. T1 is suppose working as producer and T2 is consumer. If T2 is consuming much faster then T1 is producing. What can we do so that we dont get object not found exception?

Thanks
Dimple
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
um. perhaps have thread T2 catch that exception, and put itself to sleep for a few milliseconds when it does so?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The consumer can simply call wait() to wait for products to be available. The producer can call notify() to tell the consumer that a product is ready. This is really "Threads 101". See the section on Threads in Sun's Java tutorial.

We've got a whole forum devoted to Threads here. I'll move this there for you.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java5 look at BlockingQueue. This is a nice pipeline for producers to send work to consumers. When a consumer wants work and the queue is empty the queue blocks until something comes in. If you're in an older JVM maybe read the BlockingQueue javadoc and see if you can figger out how to build one. That should be kinda fun, and I've seen some that were charmingly simple when you read the code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!