Synchronized block

Hello

Simple question (not sure of the response) :

I think that the following code means that two concurrent threads cannot execute at the same time the lockedObject in the synchronized block, is this exact ?

To be more precise, this scenario is possible ?:

1. ThreadA lock lockedObject and execute doesSomethingAOn(lockedObject);

2. ThreadB lock lockedObject and execute doesSomethingAOn(lockedObject);

3. ThreadA lock lockedObject and execute doesSomethingBOn(lockedObject);

2. ThreadB lock lockedObject and execute doesSomethingBOn(lockedObject);

Code:

...

synchronized(lockedObject)

{

doesSomethingAOn(lockedObject);

doesSomethingBOn(lockedObject);

}

This code does not mean that the synchronized block will be "atomic" (ThreadA will execute doesSomethingAOn AND doesSomethingBOn with a lock on the sequence) ?

Thanks

Ludovic

[944 byte] By [ludovicmaillet] at [2007-9-30 22:32:26]
# 1

> To be more precise, this scenario is possible ?:

> 1. ThreadA lock lockedObject and execute

> doesSomethingAOn(lockedObject);

> 2. ThreadB lock lockedObject and execute

> doesSomethingAOn(lockedObject);

Assuming lockObject references the same object in both threads, then no, its not possible. ThreadB will not be able to lock lockObject until ThreadA releases the lock by exiting the synchronized block.

> This code does not mean that the synchronized block

> will be "atomic" (ThreadA will execute

> doesSomethingAOn AND doesSomethingBOn with a lock on

> the sequence) ?

I don't know what you mean?

If every other access to every field that the doSomething... methods act upon (either directly or indirectly), is synchronized, then the synchronized block can safely be thought of as atomic.

mattbunch at 2007-7-7 12:55:46 > top of Java-index,Administration Tools,Sun Connection...
# 2

ok !

The first thread ThreadA to enter into the synchronized(lockedObject) block will lock lockedObject.

Then, all others threads (ThreadB and just after it ThreadC) will be authorized to enter into the block, too, but they will block on the first use of lockedObject in this block until ThreadA leave the lock by exiting the synchronized block.

Then, if ThreadB and ThreadC are waiting on lockedObject, the one to be authorized to execute on lockedObject will be the first to be entering into the synchronized block after ThreadA, then ThreadB.

Is this exact ?

ludovicmaillet at 2007-7-7 12:55:46 > top of Java-index,Administration Tools,Sun Connection...
# 3

> The first thread ThreadA to enter into the

> synchronized(lockedObject) block will lock

> lockedObject.

Yes.

> Then, all others threads (ThreadB and just after it

> ThreadC) will be authorized to enter into the block,

No. They will have to wait until ThreadA releases the lock, which will happen when ThreadA exits the synchronized block.

mattbunch at 2007-7-7 12:55:46 > top of Java-index,Administration Tools,Sun Connection...
# 4

ok.

Then I don't understand something....

Please have a look at this code:

synchronized (stream)

{

out = new DataOutputStream(stream);

out.writeByte(type);

out.writeInt(id);

out.writeShort(body.length);

out.write(body);

out.flush();

}

stream is shared but only in this code (client side).

this DataOutputStream instance is used only here.

When I am executing this code in multi threaded context, the sequence type-id-body.length-body is not always atomic on server side... sometimes it appears that type, id, body.length and body are mixed on reception on server side....

When I replaced this code by only one "out.writeByte(<the whole sequence type-id-body.length-body >), the problem does not appear.

Do you understand my problem even if my english is very bad, and if yes, could you explain to me what is the problem ?

Thx

Ludovic

ludovicmaillet at 2007-7-7 12:55:46 > top of Java-index,Administration Tools,Sun Connection...
# 5

Could you replace that block with this:

synchronized (stream)

{

System.out.println(Thread.currentThread().getName()+": stream = "+stream);

out = new DataOutputStream(stream);

System.out.println(Thread.currentThread().getName()+": out created");

out.writeByte(type);

System.out.println(Thread.currentThread().getName()+": type written");

out.writeInt(id);

System.out.println(Thread.currentThread().getName()+": id written");

out.writeShort(body.length);

System.out.println(Thread.currentThread().getName()+": body.length written");

out.write(body);

System.out.println(Thread.currentThread().getName()+": body written");

out.flush();

System.out.println(Thread.currentThread().getName()+": out flushed");

}

, then post the console output?

mattbunch at 2007-7-7 12:55:46 > top of Java-index,Administration Tools,Sun Connection...