The basic don't want to write, OK, directly on the Java reflection mechanism:
Class object concept: All classes have a class object that is used to provide information at the class level , such as several construction methods, how many properties, and what common methods are present.
Java class, their difference is that there are different methods, different properties, class objects, is used to describe the class, what properties, what method.
There are 3 ways of getting class objects
1. Class.forName () The full path of the class
2. Hero.class
3. New Hero (). GetClass ()
In a JVM, a class that has only one class object exists. So the above three kinds of methods taken out of the class object, are the same.
Note: It is accurate to say that a classloader, a class, will only have one class object present. Normally under a JVM, there will only be one classloader. Since the concept of ClassLoader has not been introduced, it is temporarily not expanded.
Note: When you get a class object, it causes the class property to be initialized to hero to add a static property and initialize it in the static initialization block.
Static String copyright;static {System.out.println ("Initialization of copyright"); copyright = "Copyrighted by Riot Games Company";}
Any way to get a class object will cause the static property to be initialized and will be executed only once. When synchronized modifies a static method, the synchronization object is thisClass object of the。
As in the code example, when the first thread enters method1, it takes Testreflection.class to execute.
When the second thread enters METHOD2, it can only be executed if the first thread frees up the Testreflection.class. The second thread is also required to occupy Testreflection.class. Then Testreflection.class is the Method2 synchronization object.
In other words, when a static method is modified to be synchronized, its synchronization object is the class object of the current class. Testreflection.java:
Package Com.hero;
public class Testreflection {
public static void Main (string[] args) throws Interruptedexception {
thread T1 = new Thread () {
public void Run () {
Call Method1
Testreflection.method1 ();
}
};
T1.setname ("First thread");
T1.start ();
Ensure that the first thread calls METHOD1 first
Thread.Sleep (1000);
Thread t2 = new Thread () {
public void Run () {
Call METHOD2
TESTREFLECTION.METHOD2 ();
}
};
T2.setname ("Second thread");
T2.start ();
}
Public synchronized static void Method1 () {
For Method1, the synchronization object is Testreflection.class, which only takes up testreflection.class to be executed here.
System.out.println (Thread.CurrentThread (). GetName () + "entered the Method1 method");
try {
System.out.println ("Run 5 Seconds");
Thread.Sleep (5000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
public static synchronized void Method2 () {
For MEHOTD2, there is bound to be a synchronization object, observed, when a thread in method1, occupied Testreflection.class
Can not enter the METHOD2, inferred that the Method2 synchronization object, is Testreflection.class
System.out.println (Thread.CurrentThread (). GetName () + "entered the Method2 method");
try {
System.out.println ("Run 5 Seconds");
Thread.Sleep (5000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
Testreflection2.java:
public static synchronized void Method2 () {
For MEHOTD2, there is bound to be a synchronization object, observed, when a thread in method1, occupied Testreflection.class
Can not enter the METHOD2, inferred that the Method2 synchronization object, is Testreflection.class
System.out.println (Thread.CurrentThread (). GetName () + "entered the Method2 method");
try {
System.out.println ("Run 5 Seconds");
Thread.Sleep (5000);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
The output is:
The first thread entered the Method1 method
Run for 5 seconds
(Pause in the middle for 1 seconds)
The second thread enters the Method2 method
Run for 5 seconds
However, when I change the TESTREFLECTION2.METHOD2 () to TESTREFLECTION.METHOD2 (), the pause time is 6s, so the result is obvious, locking is the class object.
Lesson Three: the Java reflection mechanism