|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
Happens before, thread start and preceding actionsHi,
The JSR 133 FAQ section on happens before (http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#synchronization) states that there is a happens-before relationship between the start of a thread and any action that happens within the thread. But what about the relationship between actions that occur before the start of a thread and those that occur after the start() of the thread? For instance, if there is an assignment to an instance-level field or a local (final) variable, and a runnable started off on a different thread within the same method following the assignment, are there visibility guarantees? To illustrate the point: class Foo{ private Object bar; void doSomething(){ bar = new Object(); final Object baz = new Object(); Thread thread = new Thread (new Runnable(){ public void run(){ if(bar != null){ //do Something with baz read(baz); } } }); } } Is it safe to assume that both bar and baz are visible to all actions within the new thread? (I'd expect there to be visibility guarantees for baz, at the minimum) Thanks, Bharath _______________________________________________ Concurrency-interest mailing list Concurrency-interest@... http://cs.oswego.edu/mailman/listinfo/concurrency-interest |
|
|
Re: Happens before, thread start and preceding actionsHi Bharath Ravi Kumar,
You have forgotten to start the thread, so there are no issues because there is no multithreading, but lets start the thread to give an answer to your question. My knowledge is not perfect, so it could be that I'm making an error. But AFAIK: There is a happens before relation between the actions before the thread.start and the thread.start (Program Order Rule) There is a happens before relation between Thread.start and Thread.run (Thread.start rule) There is a happens before relation between Thread.run and the instructions in the run (program order) Since the happens before relation is transitive, you can say: There is a happens before relation between the actions before the thread.start and the instructions in the run method. So I agree with you that it is save to assume that both bar and baz are visible to all actions within the new thread. PS: If bar is going to change after the thread is started, all bets are off. On Thu, Oct 22, 2009 at 8:42 AM, Bharath Ravi Kumar <reachbach@...> wrote: > Hi, > > The JSR 133 FAQ section on happens before > (http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html#synchronization) > states that there is a happens-before relationship between the start of a > thread and any action that happens within the thread. But what about the > relationship between actions that occur before the start of a thread and > those that occur after the start() of the thread? For instance, if there is > an assignment to an instance-level field or a local (final) variable, and a > runnable started off on a different thread within the same method following > the assignment, are there visibility guarantees? To illustrate the point: > > class Foo{ > private Object bar; > void doSomething(){ > bar = new Object(); > final Object baz = new Object(); > Thread thread = new Thread (new Runnable(){ > public void run(){ > if(bar != null){ > //do Something with baz > read(baz); > } > } > }); > } > } > Is it safe to assume that both bar and baz are visible to all actions within > the new thread? > (I'd expect there to be visibility guarantees for baz, at the minimum) > > Thanks, > Bharath > > _______________________________________________ > Concurrency-interest mailing list > Concurrency-interest@... > http://cs.oswego.edu/mailman/listinfo/concurrency-interest > > _______________________________________________ Concurrency-interest mailing list Concurrency-interest@... http://cs.oswego.edu/mailman/listinfo/concurrency-interest |
|
|
Re: Happens before, thread start and preceding actionsMy apologies. I meant to indicate that the thread was indeed being started.
-Bharath On Thu, Oct 22, 2009 at 2:25 PM, Peter Veentjer <alarmnummer@...> wrote: Hi Bharath Ravi Kumar, _______________________________________________ Concurrency-interest mailing list Concurrency-interest@... http://cs.oswego.edu/mailman/listinfo/concurrency-interest |
|
|
Re: Happens before, thread start and preceding actionsHi Peter,
Thanks for the clarification. I did expect that assignments made to bar and baz before starting the thread would be visible to actions within run(). And yes, subsequent assignments to bar aren't guaranteed to be visible to actions in the new thread (in the absence of memory barrier instructions). -Bharath On Thu, Oct 22, 2009 at 2:25 PM, Peter Veentjer <alarmnummer@...> wrote: Hi Bharath Ravi Kumar, _______________________________________________ Concurrency-interest mailing list Concurrency-interest@... http://cs.oswego.edu/mailman/listinfo/concurrency-interest |
| Free embeddable forum powered by Nabble | Forum Help |