bratao
bratao

Reputation: 2080

How can I simulate an blocking function using Java

I have two threads. One is the customer and the other is the productor.

I want to call a function in the the customer called READ. That should block indefinitely until the producer put data on it.

How can I do it ? Thanks !

Upvotes: 0

Views: 901

Answers (3)

Matt Westlake
Matt Westlake

Reputation: 3651

Use thread.wait() and then thread.notify(). thread.wait() waits until the thread.notify() happens.

Upvotes: -3

Eager
Eager

Reputation: 225

if the data to be put in the queue requires synchronization i.e. producer produces one object and stores it in a queue; consumer must take consume the object put by the producer, using SynchronousQueue or new TransferQueue should be a better idea

Upvotes: 2

Adam Sznajder
Adam Sznajder

Reputation: 9216

I would recommend you to use LinkedBlockingQueue. It's the easiest method to simulate 'producer-consument' problem. One thread puts values to the queue using put method and the other consumes data using take which is blocking (thread waits until the queue is not empty). Using LinkedBlockingQueue is very easy because you don't have to synchronize your methods. Everything is already done.

Upvotes: 5

Related Questions