views:

97

answers:

7

Threads A, B, C in that order, reach synchronized method f() in a single object.

All have the same priority.

B and C are blocked.

A leaves f().

What thread now begins running in f()? Is it always B on the principle of FIFO? Or is the order undetermined?

If C has a higher priority than B, does that guarantee that C will run rather than B?

+5  A: 

To the best of my knowledge, that's undefined.

Good thing, too. If you're writing coding that depends on priorities to determine order of execution, you're doing it wrong. You either control this stuff explicitly and intentionally, or be prepared for whatever happens. Best is to write code that will work regardless.

Priorities are a hint to the runtime scheduler, not a hard and fast directive. It's possible and legal for your priorities to be ignored altogether, or threads to be scheduled in an order having nothing to do with priorities. The best you can hope for is that, on average, your higher-priority threads get more CPU time than lesser-priority threads, other circumstances like IO waits permitting.

Carl Smotricz
Thanks, good to know that about priorities -- are you saying that (setting aside the matter of priorities) there is no guarantee of FIFO?
Joshua Fox
Correct, absolutely no guarantee. There's probably some plumbing in `java.util.concurrent` that you could use to achieve the effect programmatically, but I'm not willing to hurt my brain by exploring that question :)
Carl Smotricz
+3  A: 

The order is undefined. Having a higher priority does not guarantee first resumption. The higher priority means a thread gets more CPU time while running, but an implementation is not required to give it the lock first.

Incidentally, there is no way to write deterministic code that guarantees the A-B-C lock-entry ordering you describe. It cannot be done with the Java methods. If you write code that you think produces that ordering reliably, you haven't tested it enough. Since you cannot know in what order the threads will enter the lock, you also cannot know the order in which they'll leave it - even if there were a FIFO policy.

Borealid
Actually, higher priority doesn't even guarantee more CPU time - it is all platform specific. Distinct Java thread priorities can be mapped to the same OS priority level.
Péter Török
@Péter Török I know that - I suppose I should say "should get at least as much CPU time". Like most of threading, it's fudgy. The important part is just that it doesn't matter when waking.
Borealid
"There is no way to write deterministic code that guarantees the A-B-C lock-entry ordering." Multithreaded ordering is possible, e.g. with queues, no?
Joshua Fox
A: 

It depends on what algorithm the scheduler of your OS use. Look at this for more information about the scheduler behavior.

But you can allways do some tricks to simulate a desire order.

Ecarrion
+1  A: 

As always with threads - you never know.

Zenzen
+10  A: 

For all application intents and purposes you can assume the order is totally random. Don't play with priorities - you can easily introduce subtle priority inversion bugs that are very very very hard to catch.

Nikolai N Fetissov
+1 for this: Most people who try to use priorities to control some behavior of their program have the wrong ideas and achieve nothing but failure. Also, Java threading offers even fewer guarantees than many other implementations. Most multi-threaded programs work perfectly well with all threads at default priorities.
Carl Smotricz
Thanks, good to know that about priorities -- are you saying that (setting aside the matter of priorities) there is no guarantee of FIFO?
Joshua Fox
Not at all, it's implementation-dependent.
Nikolai N Fetissov
A: 

When all the runnable threads in the system have the same priority, the scheduler chooses the next thread to run in a simple, non-preemptive, round-robin scheduling order.

This also depends upon OS and number of CPU's. Multiple threads can be in execution if there are multiple CPU's and OS supports same.

Interesting old link :

http://journals.ecs.soton.ac.uk/java/tutorial/java/threads/priority.html

YoK
A: 

It's unspecified/platform dependent. However, Java semaphores can be fair - what guarantees FIFO.

Konrad Garus
What do yo mean by "semaphores can be fair"? Are you referring to java.util.concurrent.Semaphore and saying that while an ordinary synchronized method does not have FIFO, the Semaphore does?
Joshua Fox
Yes, exactly. Ordinary method is not guaranteed to have a FIFO (though it still may have it).
Konrad Garus