[concurrency-interest] Happens before relation between a staticwrite and a read?

David Holmes dcholmes at optusnet.com.au
Tue Feb 12 19:21:43 EST 2008


Peter,

Just to expand on what Roel said. Before a thread can use a class it has to
establish that the class has been loaded and initialized. The class
initialization protocol - JLS 12.4.2 requires that each thread first lock
the monitor associated with the class** to check its initialization state,
hence completion of the initialization of the class happens-before any other
thread sees that the class is initialized. This is why use of static
initialization is always a form of safe-publication.

** JLS 3 states this is the monitor of the Class object, but this rule has
been relaxed to be some monitor associated with the Class object by the VM.

Cheers,
David Holmes

> -----Original Message-----
> From: concurrency-interest-bounces at cs.oswego.edu
> [mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Peter
> Veentjer
> Sent: Wednesday, 13 February 2008 3:51 AM
> To: concurrency-interest
> Subject: [concurrency-interest] Happens before relation between a
> staticwrite and a read?
>
>
> I have a question regarding a happens before relation.
>
> Let me show you the example:
>
> class Foo{
>
>      private static Map somemap = new HashMap();
>
>      public void bar(){
>           somemap.put("a","b");
>      }
> }
>
> Is there a happens before relation of the write of somemap and the
> read in the bar method? My guess would be that there is no direct
> visible happens before relation, so in theory this example contains a
> data race. Is this assumption correct? I can imagine that in practice
> there is no problem because on a higher level some kind happens before
> relation is added (maybe the thread that loads the class, also starts
> the thread that executes the run methd, so  -> Thread.start happens
> before rule).
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>



More information about the Concurrency-interest mailing list