Héctor
Héctor

Reputation: 26084

What does "class loading deadlock" mean here?

I have this classes:

public class User {

    public static final NonRegisteredUser NON_REG_USER = new NonRegisteredUser();

    //...

    public static class NonRegisteredUser extends User {
        //...
    }

}

And code inspector is detecting this warning:

Referencing subclass NonRegisteredUser from superclass User initializer might lead to class loading deadlock

What does it mean exactly?

Upvotes: 15

Views: 4626

Answers (2)

Vaiden
Vaiden

Reputation: 16132

Class loader begins loading User.

Static members are init first, in order of appearance. So the class loader sees the NonRegisteredUser class, and tries loading the User class for its initialization.

Next, class loader begins loading User.

Static members are init first, in order of appearance. So the class loader sees the NonRegisteredUser class, and tries loading the User class for its initialization.

Next, class loader begins loading User.

Static members are init first, in order of appearance. So the class loader sees the NonRegisteredUser class, and tries loading the User class for its initialization...

Upvotes: 3

Kayaman
Kayaman

Reputation: 73568

The deadlock can only occur if you have 2 threads and one starts to load User and one starts to load NonRegisteredUser. There are synchronizations in place that will cause a deadlock, but then it requires separate threads. If the loading happens in a single thread, there is no deadlock as the thread owns both locks.

Hence the might in the message. However deadlocks usually do tend to require a specific environment, so there's nothing weird about that.

Upvotes: 14

Related Questions