• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in multi threading  RSS feed

 
satya sahu
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a producer consumer problem in a multi-threaded environment.

Assume that i have multiple consumer (Multiple read threads) and a
single producer(write thread).
I have a common data structure (say an int variable), being read and written into.

The write to the data sturcture happens occasionally (say at every 2 secs) but read happens contineously.

Since the read operation is contineous and done by multiple threads, making the read method synchronized will add
overhead(i.e read operation by one thread should not block the other read threads). But when ever write happens by
the write thread, that time the read operations should not be allowed.

Any ideas how to achive this ??
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want the 'int' to be broadcast to each thread, read by a thread, or is the value just that can be read by multiple threads, multiple times but you don't care how many times in which case you could just make it volatile.

When you say producer, consumer I normally think of messages coming in consumed by one of a pool of threads in which case the java.util.concurrent collections have various queues you can use.

Java has the concept of read and write locks though for efficient use apparently you need a lot more reads than writes which it sounds like you might.

java.util.concurrent.locks

... basically I was a bit confused as to what you actually wanted ;-)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!