sychronized
-
锁范围
- 普通同步方法:锁是当前实例对象this,同一个实例中的加锁方法是原子执行的
/** * sychronized范围测试 */ @Slf4j(topic = "c.TestLockScope") public class TestLockScope { public static void main(String[] args) throws InterruptedException { Resource resource = new Resource(); //线程1执行5000次加 Thread t1 = new Thread(() -> { for (int i = 0; i < 5000; i++) { resource.add(); } },"t1"); //线程2执行5000次减 Thread t2 = new Thread(() -> { for (int i = 0; i < 5000; i++) { resource.sub(); } },"t2"); t1.start(); t2.start(); t1.join(); t2.join(); //锁是当前resource实例对象,同一个对象在不同线程中的同步方法是原子执行的 //21:32:25 [main] c.TestLockScope - 结果:0 log.debug("结果:{}",resource.getNum()); } } class Resource{ private int num; public synchronized void add(){ num++; } public synchronized void sub(){ num--; } public synchronized int getNum(){ return num; } }
- 静态同步方法:锁是当前类的Class对象,使用Class创建的所有实例中的加锁方法是原子执行的
/** * sychronized范围测试 */ @Slf4j(topic = "c.TestLockScope") public class TestLockScope { public static void main(String[] args) throws InterruptedException { // Resource resource1 = new Resource(); // Resource resource2 = new Resource(); //线程1执行5000次加 Thread t1 = new Thread(() -> { for (int i = 0; i < 5000; i++) { // resource1.add(); Resource.add(); } },"t1"); //线程2执行5000次减 Thread t2 = new Thread(() -> { for (int i = 0; i < 5000; i++) { // resource2.sub(); Resource.sub(); } },"t2"); t1.start(); t2.start(); t1.join(); t2.join(); //锁是当前Resource类的Class对象,Resource类中的静态同步方法在不同线程是原子执行的 //21:35:55 [main] c.TestLockScope - 结果:0 log.debug("结果:{}",Resource.getNum()); } } class Resource{ private static int num; public static synchronized void add(){ num++; } public static synchronized void sub(){ num--; } public static synchronized int getNum(){ return num; } }
-
同步代码块:锁是()里设置的对象,可以设置当前实例对象,也可以设置类的Class对象
- 锁是当前实例对象
/** * sychronized范围测试 */ @Slf4j(topic = "c.TestLockScope") public class TestLockScope { public static void main(String[] args) throws InterruptedException { Resource resource = new Resource(); //线程1执行5000次加 Thread t1 = new Thread(() -> { for (int i = 0; i < 5000; i++) { resource.add(); } }, "t1"); //线程2执行5000次减 Thread t2 = new Thread(() -> { for (int i = 0; i < 5000; i++) { resource.sub(); } }, "t2"); t1.start(); t2.start(); t1.join(); t2.join(); log.debug("结果:{}", resource.getNum()); } } class Resource { private int num; public void add() { synchronized (this) { num++; } } public void sub() { synchronized (this) { num--; } } public int getNum() { synchronized (this) { return num; } } }
- 锁是Class对象
/** * sychronized范围测试 */ @Slf4j(topic = "c.TestLockScope") public class TestLockScope { public static void main(String[] args) throws InterruptedException { Resource resource1 = new Resource(); Resource resource2 = new Resource(); //线程1执行5000次加 Thread t1 = new Thread(() -> { for (int i = 0; i < 5000; i++) { resource1.add(); } }, "t1"); //线程2执行5000次减 Thread t2 = new Thread(() -> { for (int i = 0; i < 5000; i++) { resource2.sub(); } }, "t2"); t1.start(); t2.start(); t1.join(); t2.join(); log.debug("结果:{}", resource2.getNum()); } } class Resource { private static int num; public void add() { synchronized (Resource.class) { num++; } } public void sub() { synchronized (Resource.class) { num--; } } public int getNum() { synchronized (Resource.class) { return num; } } }