Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "class loading deadlock" mean here?

Tags:

java

android

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?

like image 265
Héctor Avatar asked Dec 28 '17 14:12

Héctor


People also ask

What is the meaning of class loading?

The parent-delegation architecture to class loading was implemented to aid security and to help programmers to write custom class loaders. Class loading loads, verifies, prepares and resolves, and initializes a class from a Java class file. Loading involves obtaining the byte array representing the Java class file.

What happens in class loading?

Class loading is done by ClassLoaders in Java which can be implemented to eagerly load a class as soon as another class references it or lazy load the class until a need of class initialization occurs. If Class is loaded before its actually being used it can sit inside before being initialized.

What is the meaning of loading a class in Java?

Java ClassLoader is used to load the classes at run time. In other words, JVM performs the linking process at runtime. Classes are loaded into the JVM according to need. If a loaded class depends on another class, that class is loaded as well. When we request to load a class, it delegates the class to its parent.

What do you know about class loading explain Java class loaders?

ClassLoader is hierarchical in loading a class into memory. Whenever a request is raised to load a class, it delegates it to the parent classloader. This is how uniqueness is maintained in the runtime environment. If the parent class loader doesn't find the class then the class loader itself tries to load the class.


1 Answers

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.

like image 117
Kayaman Avatar answered Oct 04 '22 18:10

Kayaman