Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Example from Concurrent Programming  RSS feed

 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was checking out page 100 and page 102 in Concurrent Programming in Java. I did not understand the following examples

The book says that the above example is an hand-off protocal and is thread safe. However the following example is not thread safe
How is this.

Basically the book says that if a method invocation creates an object and does not let it escape, then it can be sure that no other thread will interfere with (or even know about it) its use of that object.
It is the part Bolded above that i do not understand

[This message has been edited by Rahul Mahindrakar (edited September 14, 2000).]
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don�t have the book yet but I think that the meaning of that example is that if you have created and object and you don�t pass a reference to that object to any other thread by invoking a method of that thread that receives that reference as an argument then your object, in this case: point, is thread save.
But the example is no clear, is there any more code that you didn�t translate?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The example is given in the context of confinement, a way of organizing object accesses to prevent concurrent access of the object, without resorting to synchronization. In the first example, <code>p</code> is handed to the <code>display( Point x )</code> method at the end of <code>showNextPoint()</code>. Since <code>p</code> doesn't escape <code>showNextPoint()</code> until the end of the method, <code>p</code> is safe from concurrent access, at least in the context of <code>showNextPoint()</code>. There is no way for some other thread to access <code>p</code> at the same time as <code>showNextPoint()</code> because <code>showNextPoint()</code> performs no further access on it. (<code>showNextPoint()</code> could safely have more code after the call to <code>display()</code>, as long as the additional code did not access <code>p</code>.)
The second fragment breaks this rule by adding another access of <code>p</code> after the call to <code>display()</code>. Since <code>p</code> has already escaped (been made accessible to another method) by the time <code>recordDistance( p )</code> is called, the potential for concurrent access exists. What if <code>display( p )</code> gave some other thread (like maybe the AWT event thread) access to <code>p</code>? The second thread could access <code>p</code> at the same time as <code>recordDistance()</code> was using (possibly modifying) it.
In the technique of confinement of an object within a sequence of method calls, if it cannot be arranged for each method to follow the protocol of only handing off the object at its tail, then further measures must be taken to ensure against concurrent access. Pg. 102 continues with descriptions of the available options.
This technique requires very careful code analysis. Personally, I prefer explicit synchronization, even at some performance cost.
[This message has been edited by jply (edited September 14, 2000).]
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks jply, after reading your post three times I understand what is happening.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!