Static methods using synchronized modification.

seday10 Package;
/ **
* @author xingsir
* static method when using synchronized modification, this method must have a synchronization effect. Using the sync monitor object is "class object" static methods on this class,
* each defined class is only java (Class type of instance) only one class object
* /
public class syncDemo3 {

static void main public (String [] args) {
the Thread = T1 new new the Thread () {
public void RUN () {
Class1.method (); // static class, calling a method directly using the class name
}
};
the Thread T2 = new new the Thread () {
public void RUN () {
Class1.method (); // static class, calling a method directly using the class name
}
};
t1.start (); // start
t2.start (); // start
}

}
Class the Class1 {
/ *
* two methods:
* 1. Directly before the name of the method applied to the synchronized static void dosome public () {}
* /
public static void the synchronized Method () {
/ *
* 2. If synchronous static method monitor object, the object class can use the current class, the current class of the class object acquired directly
by: the class name .class obtained. As follows: the synchronized (Foo.class) {}
* /
// the synchronized (Foo.class) {
the try {
the Thread Thread Thread.currentThread = ();
System.out.println (Thread.getName () + ": executing method. .. ");
the Thread.sleep (5000);
System.out.println (Thread.getName () +": complete execution method ");!
} the catch (Exception E) {
e.printStackTrace ();

}
// }
}
}

Guess you like

Origin www.cnblogs.com/xingsir/p/12083706.html