views:

206

answers:

6

Dear all,

we have two threads accessing one list via a synchronized method. Can we

a) rely on the run time to make sure that each of them will receive access to the method based on the order they tried to or

b) does the VM follow any other rules

c) is there a better way to serialize the requests?

Many thanks!

A: 

Yes.

If access to the list is via one synchronized method, concurrent requests from multiple threads will be serialized.

Conrad Meyer
@Conrad. I don't think its true. When multiple threads are waiting for a monitor any one of them can be awarded depending on the implementation. We can't be sure that monitor will be awarded in the order the threads requested for it.
Varun
You are answering a subtly different question that what was asked. MrG is asking about the scheduling order of contested synchronized blocks, not whether synchronized blocks are obeyed.
Paul Wagland
Yeah, though I think it doesn't matter most of the time.
Conrad Meyer
+6  A: 

No you cannot be sure that two calls to a synchronized method will occur in order. The order is unspecified and implementation dependent.

This is defined in the 17.1 Locks section of the JLS. Notice that is says nothing about the order in which threads waiting on a lock should gain access.

Stephen C
+3  A: 

You can't rely on the order in which the particular method is called from each threads. If it is only two threads may be yes. But imagine if there are 3 threads and 1 thread already acquired access. The other 2 threads when they try to access will wait and any one of them can be awarded the access, and this does not depend on the order in which they called this method. So, it is not suggested to rely on the order.

Varun
"So, it is not suggested to rely on the order." - That's an understatement!! I'd say that any program that does rely on it is *definitely* non-portable, and *probably* buggy on the platform it was implemented / tested on!
Stephen C
@Stephen Can't agree more.
Varun
+8  A: 

No, synchronized will give access in random order (or who is the fastest to grap the Lock).

You can ensure the order by using ReentrantLock (since Java 5.0) with the fair=true option. (Lock lock = new ReentrantLock(true);)

Hardcoded
"synchronized will give access in random order (or who is the fastest to grab the Lock)". The order is simply not specified. It is almost certainly NOT random, and it is implementation dependent (and highly unlikely) that the "first" thread will always get the lock ... especially on a multiprocessor system.
Stephen C
You are right, Stephen. But you can't relay on a specific implementation, so the programmer must expect a random behaviour.
Hardcoded
I love stackoverflow because of such good answers in minutes....
DKSRathore
The order isn't random nor is it guaranteed to be random. As soon a one thread blocks it can be context switched so it won't try again for a while so you can get one thread amongst many getting the lock many times before another thread gets a chance to wake up.
Peter Lawrey
A: 

c) is there a better way to serialize the requests?

Are you by any chance using the list as a queue, i.e., does the usage pattern look something like this?

while (some condition) {
    synchronized(theList){
        anItem = get and remove an element from theList
    }
    do some work with anItem
}

If so, you may want to look at the BlockingQueue interface instead of using your own locking schemes. The implementations (like ArrayBlockingQueue) have settings for fairness and more.

gustafc
A: 

I always leave syncs to app server or engine unless defining own intensity

LarsOn