You are on page 1of 3

public class DeadlockQuiz implements Runnable{

public static final String A_CLASS_MEMBER = "A_CLASS_MEMBER:


Hello World".toString();

public static final String A_CONSTANT = "A_CONSTANT: Hello World";

public static final DeadlockQuiz INSTANCE = new DeadlockQuiz();

public static DeadlockQuiz getInstance(){


return INSTANCE;
}

private DeadlockQuiz(){
super();
launchThread();
}

public void launchThread(){


synchronized (this) {
Thread t = new Thread(this);
t.start();

while(t.isAlive()){
try{
wait(100);
}catch (InterruptedException e) {
// TODO: handle exception
}
}

}
}

public void run(){


System.out.println(A_CONSTANT);

//the thread is blocked on the following execution


System.out.println(A_CLASS_MEMBER);

public static void main(String[] args){


DeadlockQuiz.getInstance();
}
}

public static final String A_CONSTANT = "Hello World"


is a compile-time constant, When the Singleton class is loaded by a Java virtual
machine, A_CONSTANT is not stored as class variables in the initializer method
clinit. As a result, the clinit method doesn’t need to initialize it. The
A_CONSTANT field is not class variable; it is a constant, which is treated
specially by the Java compiler.
Instead of treating Singleton 's A_CONSTANT field as class variables, the Java
compiler places the constant value into the constant pool or bytecode streams of
any class that uses them. For example, if a class uses Singleton 's
A_CONSTANT field, that class will not have in its constant pool a symbolic
reference to the A_CONSTANT field of class Singleton. Instead, the class will
have operands embedded in its bytecode streams that have the value "Hello
World".

In Java code, a class or interface variable for its first active use must be
initialized, the process of setting class variables to their proper initial
values.

A proper initial value is specified via a class variable initializer or static


initializer. A class variable initializer is an equals sign and expression next to a
class variable declaration, as in:

// On CD-ROM in file classlife/ex1/Example1a.java


class Example1a {

// "= 3 * (int) (Math.random() * 5.0)" is the class variable


// initializer
static int size = 3 * (int) (Math.random() * 5.0);
}

A static initializer is a block of code introduced by the static keyword, as in:


// On CD-ROM in file classlife/ex1/Example1b.java
class Example1b {

static int size;

// This is the static initializer


static {

size = 3 * (int) (Math.random() * 5.0);


}
}

All the class variable initializers and static initializers of a type are collected by
the Java compiler and placed into one special method. For classes, this method
is called the class initialization method; for interfaces, the interface initialization
method. In Java class files for both classes and interfaces, this method is named
" clinit()". Regular methods of a Java application cannot invoke a method. This
kind of method can only be invoked by the Java virtual machine, which invokes it
to set a type's static variables to their proper initial values.

The code of the clinit() method does not explicitly invoke a superclass's clinit()
method. Before a Java virtual machine invokes the clinit() method of a class,
therefore, it must make certain the clinit() methods of superclasses have been
executed.

Note: Not all classes will necessarily have a clinit() method in their class
file. If a class declares no class variables or static initializers, it won't have
a clinit() method.

If a class declares class variables, but doesn't explicitly initialize them with
class variable initializers or static initializers, it won't invoke the clinit ()
method. If a class contains only class variable initializers for static final variables,
and those class variable initializers use compile-time constant expressions, that
class won't invoke the clinit () method. Only those classes that actually require
Java code to be executed to initialize class variables to proper initial values will
invoke the class initialization method.

Java virtual machines must also make sure the initialization process is properly
synchronized. If multiple threads need to initialize a class, only one thread should
be allowed to perform the initialization while the other threads wait. After the
active thread completes the initialization process, it must notify any waiting
threads.

You might also like