0

I have scenario where I have a shared resource and there are multiple consumers (Threads) of that resource (let say RES) . Those consumers simply consume that resource in read only manner. Similarly there is a modifier (Another Thread) which runs occasionally and update the resource RES.

I don't want my consumer threads to use resource RES if modification is in progress. To achieve this scenario I have created a monitor object which consumer has to acquire to proceed with using RES and Same is the case of modifier.

As the code is working fine but there is a possibility of optimisation, which actually is that I should not synchronize the consumer threads with each other as they are not doing any modification in the resource. What i am trying to achieve is that synchronisation between two set of threads. What I mean -

  • list TL1 which has consumer threads
  • list TL2 which has modifier threads

TL1 and TL2 should be mutually exclusive as whole but NOT the threads within TL1 and TL2.

As I could not come up with any such solution what I am trying as another option is to create let say 10 monitor objects and if consumer acquire any of the monitor object it is good to go, But modifier would have to acquire all the monitor objects before proceeding for modification. As modification is far low frequent scenario in my use case it would result in better throughput from consumer perspective.

Need suggestion on this.

Saurav Kumar Singh
  • 1,352
  • 7
  • 18
  • What it the problem with the question, could the person who downvoted explain it ? – Saurav Kumar Singh Jul 16 '19 at 06:17
  • You must synchronize consumer threads too because of visibility reasons, as they might continue to see the old value. (not my downvote) – Kartik Jul 16 '19 at 06:19
  • 1
    You can't do this with synchronized. You might be able to do it with `ReentrantLock`, which has `tryAcquire`. – Andy Turner Jul 16 '19 at 06:19
  • Not exactly, `ReentrantLock` will not block if and only if the same thread is trying to acquire the lock again. But looks like OP is expecting a set of threads to not block if they are consumers. – Andrew Scott Jul 16 '19 at 06:22
  • I changes the question title itself to make it more clear as someone read the question itself. @Kartik if I can achieve something mutual exclusion between two thread list then visibility wont be the problem any more. – Saurav Kumar Singh Jul 16 '19 at 06:23
  • @AndrewScott that's just like `synchronized`, though. Also, it also won't block if no thread currently holds the lock. – Andy Turner Jul 16 '19 at 06:25
  • I'd recommend the use of semaphores. If there are consumers executing, block the producer threads. When the value of the semaphore hits 0, meaning there are no more consumers left, allow the producers to run and block the consumers if any. – Andrew Scott Jul 16 '19 at 06:29
  • 3
    Why wouldn't the threads in TL2 have to synchronize with each other? They're modifier threads, and that usually only one thread can modify at the same time. Then they need to synchronized with each other too. It sounds like you need a `ReadWriteLock`, which allows multiple readers access at the same time, but only one writer, and if a writer has the lock, no readers can have the lock. https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantReadWriteLock.html – Erwin Bolwidt Jul 16 '19 at 06:32
  • Why not just a simple `ReadWriteLock`? Sure, if ten readers want to read, they will each lock it, (as opposed to them as group holding only a single lock somewhere), but why is that a problem? These locks are optimized for heavy concurrent use. – Thilo Jul 16 '19 at 08:53
  • 1
    Thanks @ErwinBolwidt & Thilo that is exactly what I was looking for :) – Saurav Kumar Singh Jul 16 '19 at 11:48

0 Answers0