static initializers and threads

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

static initializers and threads

teknokrat
This a question on static initializers in Java. The answer may be in the
language spec but I have not been able to find it. I have the following
class;

static {
        // do some initialization
}

class A {

        public A(){}
}

I have two threads T1, T2 executing

A a = new A();

Assuming T1 enters the static initializer first, what happens to T2?
Does it block waiting for the initialization to finish before being
given a reference to a?

Should the static block be synchronized? Are there any conditions under
which its possible to get a new object a but with the initialization
incomplete?

thanks

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: static initializers and threads

Bart Jacobs
The answer is in Section 12.4 of the Java Language Specification
<http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4>.


In fact, T2 waits for the initialization to finish before executing the
constructor of A.

There's no need to synchronize the static block. As you can read in
Section 12.4, the virtual machine performs the necessary synchronization
automatically.

The only way to get a new object of A before the completion (normal or
abrupt) of the static initializer is if the object is created from
within the static initializer itself (directly or indirectly), i.e. by
the thread that is executing the static initializer. But note that this
includes nested class initializations; for example, if the static
initializer of A triggers initialization of some class B, which in turn
creates an object of A, then this object creation succeeds and you get
an object of A prior to completion of the static initializer of A.
However, note that such a circularity could also cause a deadlock,
specifically if some other thread is already initializing B, so this
would be buggy code to write in any case.

(Note that the static block should of course be inside the class body.)

Bart

teknokrat wrote:

> This a question on static initializers in Java. The answer may be in the
> language spec but I have not been able to find it. I have the following
> class;
>
> static {
> // do some initialization
> }
>
> class A {
>
> public A(){}
> }
>
> I have two threads T1, T2 executing
>
> A a = new A();
>
> Assuming T1 enters the static initializer first, what happens to T2?
> Does it block waiting for the initialization to finish before being
> given a reference to a?
>
> Should the static block be synchronized? Are there any conditions under
> which its possible to get a new object a but with the initialization
> incomplete?
>
> thanks
>
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>  

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: static initializers and threads

teknokrat
thanks a lot. that cleared things up.

Bart Jacobs wrote:

> The answer is in Section 12.4 of the Java Language Specification
> <http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4>.
>
>
> In fact, T2 waits for the initialization to finish before executing the
> constructor of A.
>
> There's no need to synchronize the static block. As you can read in
> Section 12.4, the virtual machine performs the necessary synchronization
> automatically.
>
> The only way to get a new object of A before the completion (normal or
> abrupt) of the static initializer is if the object is created from
> within the static initializer itself (directly or indirectly), i.e. by
> the thread that is executing the static initializer. But note that this
> includes nested class initializations; for example, if the static
> initializer of A triggers initialization of some class B, which in turn
> creates an object of A, then this object creation succeeds and you get
> an object of A prior to completion of the static initializer of A.
> However, note that such a circularity could also cause a deadlock,
> specifically if some other thread is already initializing B, so this
> would be buggy code to write in any case.
>
> (Note that the static block should of course be inside the class body.)
>
> Bart
>
> teknokrat wrote:
>> This a question on static initializers in Java. The answer may be in the
>> language spec but I have not been able to find it. I have the following
>> class;
>>
>> static {
>> // do some initialization
>> }
>>
>> class A {
>>
>> public A(){}
>> }
>>
>> I have two threads T1, T2 executing
>>
>> A a = new A();
>>
>> Assuming T1 enters the static initializer first, what happens to T2?
>> Does it block waiting for the initialization to finish before being
>> given a reference to a?
>>
>> Should the static block be synchronized? Are there any conditions under
>> which its possible to get a new object a but with the initialization
>> incomplete?
>>
>> thanks
>>
>> _______________________________________________
>> Concurrency-interest mailing list
>> [hidden email]
>> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>>  
>
> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: static initializers and threads

Joshua Bloch-2
Now the quiz.  What does this program print, and why?

public class Lazy {
    private static boolean initialized = false;
    static {
        Thread t = new Thread(new Runnable() {
            public void run() {
                initialized = true;
            }
        });
        t.start();
        try {
            t.join();
        } catch (InterruptedException e) {
            throw new AssertionError(e);
        }
    }
    public static void main(String[] args) {
        System.out.println(initialized);
    }
}

For the answer, see Puzzle 85 in "Java Puzzlers."

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: static initializers and threads

Venkatesh Prasad Ranganath-2
On Thu, 2006-05-25 at 21:47 -0700, Joshua Bloch wrote:

> Now the quiz.  What does this program print, and why?
>
> public class Lazy {
>     private static boolean initialized = false;
>     static {
>         Thread t = new Thread(new Runnable() {
>             public void run() {
>                 initialized = true;
>             }
>         });
>         t.start();
>         try {
>             t.join();
>         } catch (InterruptedException e) {
>             throw new AssertionError(e);
>         }
>     }
>     public static void main(String[] args) {
>         System.out.println(initialized);
>     }
> }
>
> For the answer, see Puzzle 85 in "Java Puzzlers."

Due to the lexical ordering of static initializations and the
uninterrupted execution of the new thread and the join() invocation in
the static initializer will imply the program will print "true".  If the
latter conditions (any one) do not hold, then the program may print
"true" or "false".

For the sake of people who don't know the "Puzzlers", can you please
divulge the answer? :-)

--

Venkatesh Prasad Ranganath
_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: static initializers and threads

Hanson Char
> For the sake of people who don't know the "Puzzlers", can you please
> divulge the answer? :-)

For the sake of not killing the fun, please don't.

(Venkatesh, have you tried running the code to see ?)

H

On 5/26/06, Venkatesh Prasad Ranganath <[hidden email]> wrote:

> On Thu, 2006-05-25 at 21:47 -0700, Joshua Bloch wrote:
> > Now the quiz.  What does this program print, and why?
> >
> > public class Lazy {
> >     private static boolean initialized = false;
> >     static {
> >         Thread t = new Thread(new Runnable() {
> >             public void run() {
> >                 initialized = true;
> >             }
> >         });
> >         t.start();
> >         try {
> >             t.join();
> >         } catch (InterruptedException e) {
> >             throw new AssertionError(e);
> >         }
> >     }
> >     public static void main(String[] args) {
> >         System.out.println(initialized);
> >     }
> > }
> >
> > For the answer, see Puzzle 85 in "Java Puzzlers."
>
> Due to the lexical ordering of static initializations and the
> uninterrupted execution of the new thread and the join() invocation in
> the static initializer will imply the program will print "true".  If the
> latter conditions (any one) do not hold, then the program may print
> "true" or "false".
>
> For the sake of people who don't know the "Puzzlers", can you please
> divulge the answer? :-)
>
> --
>
> Venkatesh Prasad Ranganath
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: static initializers and threads

Joshua Bloch-2
I'm gonna go with Hanson on this.  If anyone has run the program,
can't figure out why it does what it does, and asks me, I will send
them the solution, but I won't post it for a few days:)

            Josh

On 5/26/06, Hanson Char <[hidden email]> wrote:

> > For the sake of people who don't know the "Puzzlers", can you please
> > divulge the answer? :-)
>
> For the sake of not killing the fun, please don't.
>
> (Venkatesh, have you tried running the code to see ?)
>
> H
>
> On 5/26/06, Venkatesh Prasad Ranganath <[hidden email]> wrote:
> > On Thu, 2006-05-25 at 21:47 -0700, Joshua Bloch wrote:
> > > Now the quiz.  What does this program print, and why?
> > >
> > > public class Lazy {
> > >     private static boolean initialized = false;
> > >     static {
> > >         Thread t = new Thread(new Runnable() {
> > >             public void run() {
> > >                 initialized = true;
> > >             }
> > >         });
> > >         t.start();
> > >         try {
> > >             t.join();
> > >         } catch (InterruptedException e) {
> > >             throw new AssertionError(e);
> > >         }
> > >     }
> > >     public static void main(String[] args) {
> > >         System.out.println(initialized);
> > >     }
> > > }
> > >
> > > For the answer, see Puzzle 85 in "Java Puzzlers."
> >
> > Due to the lexical ordering of static initializations and the
> > uninterrupted execution of the new thread and the join() invocation in
> > the static initializer will imply the program will print "true".  If the
> > latter conditions (any one) do not hold, then the program may print
> > "true" or "false".
> >
> > For the sake of people who don't know the "Puzzlers", can you please
> > divulge the answer? :-)
> >
> > --
> >
> > Venkatesh Prasad Ranganath
> > _______________________________________________
> > Concurrency-interest mailing list
> > [hidden email]
> > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> >
>

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: static initializers and threads

Dhanji R. Prasanna
Correct me if Im wrong but I think the answer is in the order of the
initializers. The doco says static blocks must complete before static
fields. So when the anonymous class thread starts, it enters run and
attempts to set initialized to true.
However initialized has not been initialized (!) yet because the
static block is still incomplete (waiting for the thread to die on
t.join()), this causes a deadlock and the program never terminates
(never even gets to main body so nothing is printed).

Btw are you withholding the solution for the pure intellectual
titillation it provides us or so a few of us will dash out to amazon
for more copeis of puzzlers, josh?

jk ;)

On 5/27/06, Joshua Bloch <[hidden email]> wrote:

> I'm gonna go with Hanson on this.  If anyone has run the program,
> can't figure out why it does what it does, and asks me, I will send
> them the solution, but I won't post it for a few days:)
>
>             Josh
>
> On 5/26/06, Hanson Char <[hidden email]> wrote:
> > > For the sake of people who don't know the "Puzzlers", can you please
> > > divulge the answer? :-)
> >
> > For the sake of not killing the fun, please don't.
> >
> > (Venkatesh, have you tried running the code to see ?)
> >
> > H
> >
> > On 5/26/06, Venkatesh Prasad Ranganath <[hidden email]> wrote:
> > > On Thu, 2006-05-25 at 21:47 -0700, Joshua Bloch wrote:
> > > > Now the quiz.  What does this program print, and why?
> > > >
> > > > public class Lazy {
> > > >     private static boolean initialized = false;
> > > >     static {
> > > >         Thread t = new Thread(new Runnable() {
> > > >             public void run() {
> > > >                 initialized = true;
> > > >             }
> > > >         });
> > > >         t.start();
> > > >         try {
> > > >             t.join();
> > > >         } catch (InterruptedException e) {
> > > >             throw new AssertionError(e);
> > > >         }
> > > >     }
> > > >     public static void main(String[] args) {
> > > >         System.out.println(initialized);
> > > >     }
> > > > }
> > > >
> > > > For the answer, see Puzzle 85 in "Java Puzzlers."
> > >
> > > Due to the lexical ordering of static initializations and the
> > > uninterrupted execution of the new thread and the join() invocation in
> > > the static initializer will imply the program will print "true".  If the
> > > latter conditions (any one) do not hold, then the program may print
> > > "true" or "false".
> > >
> > > For the sake of people who don't know the "Puzzlers", can you please
> > > divulge the answer? :-)
> > >
> > > --
> > >
> > > Venkatesh Prasad Ranganath
> > > _______________________________________________
> > > Concurrency-interest mailing list
> > > [hidden email]
> > > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> > >
> >
>
> _______________________________________________
> Concurrency-interest mailing list
> [hidden email]
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: static initializers and threads

Joshua Bloch-2
Dhanji,


> Btw are you withholding the solution for the pure intellectual
> titillation it provides us or so a few of us will dash out to amazon
> for more copeis of puzzlers, josh?

Intellectual titillation, of course.  Would Click and Hack the Type-It
Brothers stoop to shameless commerce? ;)

Perhaps it is time to post the solution.  There have been a number of
partially correct replies.  The next letter from me will contain the
complete solution.  It will have ***SPOILER*** in its subject.  Don't
read it if you still want to puzzle it out.

       Josh

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: static initializers and threads

Venkatesh Prasad Ranganath-2
In reply to this post by Hanson Char
On Fri, 2006-05-26 at 13:25 -0700, Hanson Char wrote:
> > For the sake of people who don't know the "Puzzlers", can you please
> > divulge the answer? :-)
>
> For the sake of not killing the fun, please don't.
>
> (Venkatesh, have you tried running the code to see ?)

Since it was a puzzler, I did not execute the code.  

Josh, thanks for posting the solution.  

"Puzzlers" is now on my to-read list.

 - VPR

> H
>
> On 5/26/06, Venkatesh Prasad Ranganath <[hidden email]> wrote:
> > On Thu, 2006-05-25 at 21:47 -0700, Joshua Bloch wrote:
> > > Now the quiz.  What does this program print, and why?
> > >
> > > public class Lazy {
> > >     private static boolean initialized = false;
> > >     static {
> > >         Thread t = new Thread(new Runnable() {
> > >             public void run() {
> > >                 initialized = true;
> > >             }
> > >         });
> > >         t.start();
> > >         try {
> > >             t.join();
> > >         } catch (InterruptedException e) {
> > >             throw new AssertionError(e);
> > >         }
> > >     }
> > >     public static void main(String[] args) {
> > >         System.out.println(initialized);
> > >     }
> > > }
> > >
> > > For the answer, see Puzzle 85 in "Java Puzzlers."
> >
> > Due to the lexical ordering of static initializations and the
> > uninterrupted execution of the new thread and the join() invocation in
> > the static initializer will imply the program will print "true".  If the
> > latter conditions (any one) do not hold, then the program may print
> > "true" or "false".
> >
> > For the sake of people who don't know the "Puzzlers", can you please
> > divulge the answer? :-)
> >
> > --
> >
> > Venkatesh Prasad Ranganath
> > _______________________________________________
> > Concurrency-interest mailing list
> > [hidden email]
> > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> >

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
Reply | Threaded
Open this post in threaded view
|

Re: static initializers and threads

Joshua Bloch-2
Venkatesh,
 
Great.  Enjoy the book:)
 
    Josh

 
On 5/28/06, Venkatesh Prasad Ranganath <[hidden email]> wrote:
On Fri, 2006-05-26 at 13:25 -0700, Hanson Char wrote:
> > For the sake of people who don't know the "Puzzlers", can you please
> > divulge the answer? :-)
>
> For the sake of not killing the fun, please don't.
>
> (Venkatesh, have you tried running the code to see ?)

Since it was a puzzler, I did not execute the code.

Josh, thanks for posting the solution.

"Puzzlers" is now on my to-read list.

- VPR

> H
>
> On 5/26/06, Venkatesh Prasad Ranganath <[hidden email]> wrote:
> > On Thu, 2006-05-25 at 21:47 -0700, Joshua Bloch wrote:
> > > Now the quiz.  What does this program print, and why?
> > >
> > > public class Lazy {
> > >     private static boolean initialized = false;
> > >     static {
> > >         Thread t = new Thread(new Runnable() {
> > >             public void run() {
> > >                 initialized = true;
> > >             }
> > >         });
> > >         t.start();
> > >         try {
> > >             t.join();
> > >         } catch (InterruptedException e) {
> > >             throw new AssertionError(e);
> > >         }
> > >     }
> > >     public static void main(String[] args) {
> > >         System.out.println(initialized);
> > >     }
> > > }
> > >
> > > For the answer, see Puzzle 85 in "Java Puzzlers."
> >
> > Due to the lexical ordering of static initializations and the
> > uninterrupted execution of the new thread and the join() invocation in
> > the static initializer will imply the program will print "true".  If the
> > latter conditions (any one) do not hold, then the program may print
> > "true" or "false".
> >
> > For the sake of people who don't know the "Puzzlers", can you please
> > divulge the answer? :-)
> >
> > --
> >
> > Venkatesh Prasad Ranganath
> > _______________________________________________
> > Concurrency-interest mailing list
> > [hidden email]
> > http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
> >

_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest


_______________________________________________
Concurrency-interest mailing list
[hidden email]
http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest