Doubt in Concurrent Object chapter about final fields in java.

109 views
Skip to first unread message

Pushparaj Motamari

unread,
May 11, 2016, 4:08:19 AM5/11/16
to Art of Multiprocessor Programming
Hi,

On page 64, Chapter 3- Concurrent Objects there is an example below

Public class EventListener {

final int x;

public EventListener(EventSource source){

eventSource.registerListener(source);

}

public onEvent(Event e){
..//handle the event

}

}

As per the book above class is incorrect. And it says following text "This code may appear safe, since registration is the last step in the constructor, but it is incorrect, because if another  thread calls the method onEvent() method before constructor finishes,then the onEvent() methos is not guaranteed to see a correct value for x"

The simulation code can be

public class TestClass {
private EventListener listener2;

public static void main(String[] args) {
TestClass f1 = new TestClass();
listenerThread lThread = f1.new listenerThread();
initializerThread iThread = f1.new initializerThread();
lThread.start();
iThread.start();
}

class listenerThread extends Thread {
@Override
public void run() {
while (true) {
if(listener2!=null){
listener2.onEvent(new Event("hello"));
}
}

}
}

class initializerThread extends Thread {
@Override
public void run() {
while (true) {
if (listener2 == null) {
listener2 = new EventListener();
}
}
}
}
}

My doubt is as per Java Language specification https://docs.oracle.com/javase/specs/jls/se8/html/jls-12.html#jls-12.5 the result of new EventListener() will not
be available until the constructor creation completes which means variable x will get initialized before variable listener2 is assigned a value other than null.
Then there is no way listenerThread can see different value for x. Is there any other way to reproduce the issue the other mentions here..i.e. another thread seeing
different value for x.

Chris Uzdavinis

unread,
May 11, 2016, 10:04:05 AM5/11/16
to art-of-multiproc...@googlegroups.com
It might be worth noting that in my 2008 copy of the book the code you
posted is slightly different (changes are bold) but the difference
is significant, since it exposes the "this" parameter to external
code:

public EventListener(EventSource eventSource){
  eventSource.registerListener(this);
}

Since final "x' must be initialized somewhere or the code will not
compile, we must assume it's initialized but that initialization
is not shown.  (Unfortunately, as that makes this a bad example
since x is the focus!)

I think the book is still correct because the only
guarantee you have about the event listener object is that when
the constructor has completed, the following 2 things have been
done:

A) a value was written to x
B) the "this" parameter was written into eventSource's collection of listeners.

BUT there is is no guarantee they are implemented in this order!  If B happens first,
then another thread may end up seeing the object before the constructor
has finished A, and the wrong value of x is possible.

chris

--
You received this message because you are subscribed to the Google Groups "Art of Multiprocessor Programming" group.
To unsubscribe from this group and stop receiving emails from it, send an email to art-of-multiprocessor-...@googlegroups.com.
To post to this group, send email to art-of-multiproc...@googlegroups.com.
Visit this group at https://groups.google.com/group/art-of-multiprocessor-programming.
For more options, visit https://groups.google.com/d/optout.

Pushparaj Motamari

unread,
May 13, 2016, 4:34:16 AM5/13/16
to Art of Multiprocessor Programming
Hi,

Thank you for the reply. I have understood the concept.
Reply all
Reply to author
Forward
0 new messages