Efficient synchronization of querying an array of resources

Posted by Erel Segal Halevi on Stack Overflow See other posts from Stack Overflow or by Erel Segal Halevi
Published on 2012-06-04T10:36:37Z Indexed on 2012/06/04 10:41 UTC
Read the original article Hit count: 258

Filed under:
|

There is a list of N resources, each of them can be queried by at most a single thread at a time.

There are serveral threads that need to do the same thing at approximately the same time: query each of the resources (each thread has a different query), in arbitrary order, and collect the responses.

If each thread loops over the resources in the same order, from 0 to N-1, then they will probably have to wait for each other, which is not efficient.

I thought of letting the threads loop over the resources in a random permutation, but this seems too complex and also not so efficient, for example, for 2 resources and 2 threads, in half the cases they will choose the same order and wait for each other.

Is there a simple and more efficient way to solve this?

© Stack Overflow or respective owner

Related posts about java

Related posts about synchronization