Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6148827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:20:49+00:00 2026-05-23T19:20:49+00:00

I came across a deadlock scenario which can be summarized as the StaticDeadlock class

  • 0

I came across a deadlock scenario which can be summarized as the StaticDeadlock class shown below.

This simple program will freeze at o.getClass(). Here’s my speculation of what happened, but can someone explain it better?

1) the program enters StaticDeadlock static block

2) thread starts

3) main thread is put in wait for thread to finish, hence can’t finish the static block

4) inside thread it access StaticDeadlock.o but StaticDeadlock’s static block is not finished yet. Hence the program freezes?

    public class StaticDeadlock
    {
        private static final Object o = new Object();

        static {
            MyThread thread = new MyThread();
            thread.start();

            try {
                thread.join();
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public static void main (String[] args)
        {
            System.out.println("all is well.");
        }

        static class MyThread extends Thread
        {
            @Override
            public void run ()
            {
                System.out.println("inside mythread");
                o.getClass();
            }
        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:20:49+00:00Added an answer on May 23, 2026 at 7:20 pm

    Yes, that’s pretty much it. The new thread is waiting for the class initializer of StaticDeadlock to complete before it accesses the static member. See section 12.4.2 of the Java Language Specification for more details, in particular these steps:

    1. Synchronize (§14.19) on the Class object that represents the class or interface to be initialized. This involves waiting until the current thread can obtain the lock for that object (§17.1).

    2. If initialization is in progress for the class or interface by some other thread, then wait on this Class object (which temporarily releases the lock). When the current thread awakens from the wait, repeat this step.

    3. If initialization is in progress for the class or interface by the current thread, then this must be a recursive request for initialization. Release the lock on the Class object and complete normally.

    4. If the class or interface has already been initialized, then no further action is required. Release the lock on the Class object and complete normally.

    It won’t even get past step 1 in the second thread, as the first thread has the lock and won’t release it.

    Note that it’s not calling getClass() which causes the problem – doing anything which requires the value of o will make the second thread wait until the class initializer has completed, which of course won’t happen because the first thread is waiting for the second thread to finish.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this program and its not behaving in expected way. public class
Came across this error today. Wondering if anyone can tell me what it means:
I came across this class while reading a C# book and have some questions.
I came across this syntax which i have not seen before struct A {
I came across a few articles like this one , which suggest that some
I came across this question about memory management of dictionaries, which mentions the intern
I came across this thread Node.js HTTPS Secure Error which mentions that the tls
I came across this piece of code, and I can't quite figure out why
Came across something like this today, and was wondering if there was an equivalent
I came across this article written by Andrei Alexandrescu and Petru Marginean many years

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.