File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes How to handle Concurrency of multiple threads removing and adding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "How to handle Concurrency of multiple threads removing and adding" Watch "How to handle Concurrency of multiple threads removing and adding" New topic

How to handle Concurrency of multiple threads removing and adding

Priyanka Dandekar
Ranch Hand

Joined: Aug 06, 2008
Posts: 52
I am trying to come up with a data structure where multiple threads can add elements to my data structure and multiple threads can remove the data from my data structure.

I am trying to create a data structure like queue where multiple threads can add and delete the data from queue.

Should I keep add and remove methods synchronized?

If I synchronize these methods then only 1 thread can add element at a time which may slow down the performance.

Let me know what could be the best way to implement it with good performance.

Any pointers or suggestions are welcome.

Priya - Mockup Tool | Java | Struts2 | Lucene
Sachin Joshi
Ranch Hand

Joined: Aug 06, 2008
Posts: 83

What Java version you are using? If its JDK1.5 or later then you should look at the java.util.concurrent package. JDK API

I guess You must be looking for something like a BlockingQueue, there are many implementation of BlockingQueue available and if you want you can write your own by implementing BlockingQueue interface.

Here is a example how you can use the ArrayBlockingQueue in multi threaded environment.

Web Development Tips and Tutorials - By Sachin
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

It depends a lot on your use-case. As Sachin said, you could use one of the BlockingQueues, which are most appropriate for a publish/consume scenario, where some Threads puts things into the Queue and others takes them out.

Another option if you have multiple threads that both publish into and consume from the queue would be to do what the 'Synchronized' collections do (like Vector, Hashtable, and those returned from Collections.synchronizedXXX() methods), which is to synchronize all access methods to the Queue which prevents simultaneous gets and puts. If you do this, you might think about using the Collections.synchronizedCollection() method - maybe it is good enough. Otherwise I would write a wrapper Object. But like you said, all access will be done one at a time which can slow things down when there are a lot of simultaneous accesses, and iterating over the collection would not be thread safe - you would have to add additional synchronization.

The third option would be to use a class like ConcurrentLinkedQueue which allows concurrent access to the queue in a thread safe, non-blocking manner. This also requires Java 1.5+

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46375
Looks too difficult for "beginning Java." Moving.
I agree. Here's the link:
subject: How to handle Concurrency of multiple threads removing and adding
It's not a secret anymore!