Static synchronization of threads and non-static differences
So what is the difference between adding synchronized to the static and non-static methods?
As we all know, the static method belongs to the Class method, and it belongs to this Class (Note: The Class here does not refer to a specific pair of Class
Such as), the lock obtained by static is the Class of the object currently calling this method (Class, instead of produced by this Class ).
). Instead of the lock obtained by the static method, it is the lock of the object currently calling this method. So no between them
Will generate mutex.
package com.jack.zhang.chapter9.classlock;/** * * @author EX-LIUQI001 * */public class Test {public static synchronized void staticX() throws InterruptedException {for (int i = 0; i < 10; i++) {Thread.sleep(1000);System.out.println("staticX.......................");}}public synchronized void x() throws InterruptedException {for (int i = 0; i < 10; 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 Auto-generated catch blocke.printStackTrace();}}}, "a");Thread thread1 = new Thread(new Runnable() {public void run() {try {Test.staticX();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}, "b");thread1.start();thread.start();}}
The running result is:
StaticX .......................
X .......................
X .......................
StaticX .......................
StaticX .......................
X .......................
X .......................
StaticX .......................
X .......................
StaticX .......................
StaticX .......................
X .......................
X .......................
StaticX .......................
X .......................
StaticX .......................
X .......................
StaticX .......................
X .......................
StaticX .......................
What should we do when we want to synchronize all objects under this class, that is, to share the same lock for all objects under this class?
Check the Code:
package com.jack.zhang.chapter9.classlock;/** * * @author EX-LIUQI001 * */public class Test2 {public final static Byte[] locks = new Byte[0];public 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 < 10; 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 Auto-generated catch blocke.printStackTrace();}}}, "a");Thread thread1 = new Thread(new Runnable() {public void run() {try {Test2.staticX();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}, "b");thread1.start();thread.start();}}
Running result:
StaticX .......................
StaticX .......................
StaticX .......................
StaticX .......................
StaticX .......................
StaticX .......................
StaticX .......................
StaticX .......................
StaticX .......................
StaticX .......................
X .......................
X .......................
X .......................
X .......................
X .......................
X .......................
X .......................
X .......................
X .......................
X .......................