So what's the difference between static and non-static methods before adding synchronized?
As we all know, the static method belongs to the class method, it belongs to this class (note: The class here does not mean a specific pair of class
), then the lock acquired by Static is the class (class) of the object that is currently calling this method, and is no longer generated by this class
of a specific object). Instead of the lock acquired by the static method, it is the lock of the object that is currently calling this method. So they don't
will result in mutual exclusion.
package com.jack.zhang.chapter9.classlock;/** * * @author ex-liuqi001 * */public class Test {public static synchronized void STA Ticx () throws interruptedexception {for (int i = 0; i <; i++) {thread.sleep (1000); System.out.println ("Staticx ..................) public synchronized void X () throws interruptedexception {for (int i = 0; i < x i++) {thread.sleep (1000); System.out.println ("x ..... ................ public static void Main (string[] args) {final Test test1 = new test (); Thread thread = new Thread (new Runnable () {public void run () {try {test1.x ()} catch (Interruptedexception e) {//TODO Aut O-generated catch Blocke.printstacktrace ();}}}, "a"); Thread thread1 = new Thread (new Runnable () {public void run () {try {test.staticx ()}, catch (Interruptedexception e) {//To Do auto-generated catch Blocke.printstacktrace ();}}, "B"); Thread1.start (); Thread.Start ();}}
The operating result is:
Staticx ........ .........
X.......................
X.......................
Staticx ........ .........
Staticx ........ .........
X.......................
X.......................
Staticx ........ .........
X.......................
Staticx ........ .........
Staticx ........ .........
X.......................
X.......................
Staticx ........ .........
X.......................
Staticx ........ .........
X.......................
Staticx ........ .........
X.......................
Staticx ........ .........
What do we do when we want all the objects under this class to be synchronized, that is, to have all the objects under this class share the same lock?
Look at the code:
Package com.jack.zhang.chapter9.classlock;/** * * @author ex-liuqi001 * */public class Test2 {public final static byte[] Locks = new Byte[0];p ublic static void Staticx () throws Interruptedexception {synchronized (locks) {for (int i = 0; I < ; 10; i++) {thread.sleep (1000); System.out.println ("Staticx ..................) public void X () throws Interruptedexception {synchronized (locks) {for (int i = 0; i <; i++) {thread.sleep (1000); System.out.println ("x ..... ...............) public static void Main (string[] args) {final Test2 test1 = new Test2 (); Thread thread = new Thread (new Runnable () {public void run () {try {test1.x ()} catch (Interruptedexception e) {//TODO Aut O-generated catch Blocke.printstacktrace ();}}}, "a"); Thread thread1 = new Thread (new Runnable () {public void run () {try {test2.staticx ()} catch (Interruptedexception e) {//T ODO auto-generated catch Blocke.printstacktrace ();}}, "B"); Thread1.start (); Thread.Start ();}}
Operation Result:
Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
Staticx ........ .........
X.......................
X.......................
X.......................
X.......................
X.......................
X.......................
X.......................
X.......................
X.......................
X.......................
Thread static synchronization vs. non-static differences