开发者

Java Concurrency: lock effiency

My program has 100 threads.

Every single thread does this:

1) if arrayList is empty, add element with certain properties to it

2) if arrayList is not empty, iterate through elements found in arrayList, if found suitable element (matching certain properties), get it and remove the arrayList

The problem here is that while one thread is iterating through the arrayList, other 99 threads are waiting for the lock on arrayList.

What would you suggest to me if I want all 100 threads to work in lock开发者_StackOverflow社区-less condition? So they all have work to do?

Thanks


Have you looked at shared vs exclusive locking? You could use a shared lock on the list, and then have a 'deleted' property on the list elements. The predicate you use to check the list elements would need to make sure the element is not marked 'deleted' in addition to whatever other queries you have - also due to potential read-write conflicts, you would need to lock on each element as you traverse. Then periodically get an exclusive lock on the list to perform the deletes for real.

The read lock allows for a lot of concurrency on the list. The exclusive locks on each element of the list are not as nice, but you need to force the memory model to update your 'deleted' flag to each thread, so there's no way around that.


First if you're not running on a machine that has 64 cores or more your 100 threads are probably a performance hog in themselves.

Then an ArrayList for what you're describing is certainly not a good choice because removing an element does not run in amortized constant time but in linear time O(n). So that's a second performance hog. You probably want to use a LinkedList instead of your ArrayList (if you insist on using a List).

Now of course I doubt very much that you need to iterate over your complete list each time you need to find one element: wouldn't another data structure be more appropriate? Maybe that the elements that you put in your list have such a concept as "equality" and hence a Map with an O(1) lookup time could be used instead?

That's just for a start: as I showed you, there are at least two serious performances issues in what you described.... Maybe you should clarify your question if you want more help.


If your notion of "suitable element (matching certain properties)" can be encoded using a Comparator then a PriorityBlockingQueue would allow each thread to poll the queue, taking the next element without having to search the list or enqueuing a new element if the queue is empty.

Addendum: Thilo raise an essential point: As your approach evolves, you may want to determine empirically how many threads are optimal.


The key is to only use the object lock on arraylist when you actually need to.

A good idea would be to subclass arraylist and provide synchro on single read + write + delete processes.

This will ensure fine granularity with the locking while allowing the threads to run through the array list while protecting the semantics of the arraylist.


Have a single thread own the array and be responsible for adding to it and iterating over it to find work to do. Once a unit of work is found, put the work on a BlockingQueue. Have all your worker threads use take() to remove work from the queue.

This allows multiple units of work to be discovered per pass through the array and they can be handed off to waiting worker threads fairly efficiently.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜