Threads and synchronization

J

Joshua Cranmer

Lasse said:
The class has int field called "i". As an int field, its initial value
is zero, and it is not set to 100 until after the superclass' constructor
has run. That would be Object's default constructor, which doesn't do
a lot, but it's there.
I.e., at some point the value is zero.

I grant you that.
The other thread, when started, accesses the value of the field from a
different thread than where it was assigned the values 100 and 101,
without any synchronization. Therefore the other thread should also
be able to see the stale value "0".

True, but thread startup is a synchronization event, as far as I can
read from the JLS memory model. Therefore the second thread is forced to
recognize at least the 100.
 
L

Lasse Reichstein Nielsen

Joshua Cranmer said:
True, but thread startup is a synchronization event, as far as I can
read from the JLS memory model. Therefore the second thread is forced
to recognize at least the 100.

Ack, true. I had missed the implicit synchronization between a thread
starter and the started thread.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top