Thread synchronization question: queued or not?
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Amit_Rosner
Posted On:   Tuesday, January 1, 2002 08:49 AM

Hi! I have a synchronization question: I have the following code: int waiting = 0; object obj = new Object(); public void foo() { waiting++; synchronized (obj) { waiting--; ... } } Of course that when many threads try to enter that method, only the first will succeed and that other will wait at the synchronized block. My question is that: Suppose threads A, B, C, D, E arrive at this order and thread A captures the obj Object's monitor at the synchronized block. Meanwhile    More>>

Hi!



I have a synchronization question:



I have the following code:



int waiting = 0;

object obj = new Object();



public void foo()

{

waiting++;



synchronized (obj)

{

waiting--;



...

}

}



Of course that when many threads try to enter that method, only the
first will succeed and that other will wait at the synchronized block.



My question is that: Suppose threads A, B, C, D, E arrive at this order
and thread A captures the obj Object's monitor at the synchronized block.
Meanwhile thread B is waiting, and so is C, D, E (B came first, and so on).



When A releases the monitor at the end of the synchronized block - to the
waiting threads enter it in a queue manner (i.e. B, then when B is done C,
and so on) - or is it random?



I have tested that over and over
again, because I thought that the order is random, but in every time I saw (with debug prints)
that is had the behaviour of a queue. There
wasn't a single time it went otherwise.



Since it is important for me to have it as a queue, I really need to know whether that is the defined
behaviour or maybe that was just incident.



Can anyone help me?

   <<Less

Re: Thread synchronization question: queued or not?

Posted By:   Marian_Olteanu  
Posted On:   Thursday, February 28, 2002 06:23 AM

Quote from API documentation, java.lang.Object , method notify() :


Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation.

Re: Thread synchronization question: queued or not?

Posted By:   Bozidar_Dangubic  
Posted On:   Wednesday, January 2, 2002 06:16 AM

if you need it as a queue, write it as a queue. create a ThreadPool class which handles your worker threads for you. you can also download the code for this from the web, many have already been written. this way, you are controlling how threads are allocated and in which order and you are not depending on the JVM to implement thread pooling for you. it also allows you to change priority policy later (from FIFO to say LIFO) in a single location.

Re: Thread synchronization question: queued or not?

Posted By:   Terry_Laurenzo  
Posted On:   Tuesday, January 1, 2002 09:44 AM

I looked briefly for the relevant portion in the JVM spec, but I couldn't find it. However, I am 99% sure that the ordering that you are observing is not mandated by the spec. It is therefore subject to change and should not be relied on(even using a multi-processor machine instead of a single processor one could affect it). I too have observed this "queue-like" behavior of waiting threads on the win32 sun VMs, but I cannot speak for the others.

Without knowing what you are trying to accomplish that requires this ordering, I really cannot spell out a solution. There are a number of ways to ensure ordering, varying from simple(and usually inefficient) to complex.
About | Sitemap | Contact