• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

synchronization problem

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello friends please see the code below..Here I am using executors for multi-threading....

My problem is that I am acquiring lock on the list so that in consumer thread only one thread should be allowed to remove the element from list...but it has a drawback I cannot add an item in same list in producer as it might happen that list lock in taken by one of the consumer thread...
so I want that producer should be able to add in the list but only one consumer should be able to remove from it.. any suggestions...please note that there are 3 consumer threads and 1 producer thread



[Added code tags - see UseCodeTags for details]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryan Raina wrote:hello friends please see the code below..Here I am using executors for multi-threading....

My problem is that I am acquiring lock on the list so that in consumer thread only one thread should be allowed to remove the element from list...but it has a drawback I cannot add an item in same list in producer as it might happen that list lock in taken by one of the consumer thread...
so I want that producer should be able to add in the list but only one consumer should be able to remove from it.. any suggestions...please note that there are 3 consumer threads and 1 producer thread



Well.... One option is to *not* grab the lock on the list (for the whole operation). Just use the Collections.synchronizedList() method to return a thread safe list, which will allow the producer and consumers to both work on the list concurrently. Of course, this option will not stop the consumers from working concurrently. To solve that you can use the same technique, but with a different object. Have the consumers grab the lock of a different object -- that the producer will not grab. Only one consumer will own the lock, and it will work concurrently with the producer(s).

Henry
 
Ryan Raina
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for you help !!!
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic