java 更正对synchronized的一个错误认识
用一个问题来开启这篇文章:“java中两个线程是否可以同是否问一个对象的两个不同的synchronized方法?”
参考:http://www.jianshu.com/p/f23a90a79b3a(每个人都说自己是原创的…我就选了个排版最漂亮的)
一、问题的解答
我之前一直认为:
几个不同线程同时访问某个类的同一个synchronized方法时, 当一个线程执行时, 其他线程都等待; 若是几个不同线程访问的是一个类不同的synchronized, 则可以同时执行。
之前我一直是这么认为的,现在来做个试验:
Test.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
package com.test.testSynchronized; public class Test { public synchronized void method1() { long start = System.currentTimeMillis(); System.out.println("method1 get the lock"); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("method1 release the lock"); System.out.println("method1 costs " + (System.currentTimeMillis() - start)); } public synchronized void method2() { long start = System.currentTimeMillis(); System.out.println("method2 get the lock"); System.out.println("method2 release the lock"); System.out.println("method2 costs " + (System.currentTimeMillis() - start)); } public static void main(String[] args) throws InterruptedException { Test test = new Test(); Thread1 thread1 = new Thread1(test); Thread2 thread2 = new Thread2(test); thread1.start(); // 先执行, 以便抢占锁 Thread.sleep(500); // 放弃cpu, 让thread1执行, 以便获的锁 thread2.start(); } } class Thread1 extends Thread { Test test; public Thread1(Test test) { this.test = test; } @Override public void run() { test.method1(); } } class Thread2 extends Thread { Test test; public Thread2(Test test) { this.test = test; } @Override public void run() { test.method2(); } } |
Test类中有method1、method2两个同步方法,这两个方法的区别在于:method1执行时睡眠5s,而method2立刻执行。现在我希望开启两条线程,分别调用这两个同步方法。我会让Thread1调用method1、让Thread2调用method2(main方法中,Thread1先执行,Thread2后执行)。
如果之前的结论是正确的,那么两条线程就可以并发执行,我们应该会看到:在Thread1睡眠期间,Thread2首先执行完毕,Thread1随后苏醒(个人理解:注意这里是“苏醒”,是sleep状态恢复成运行态,要和notify方法的“唤醒”区分开来),后执行完毕。
真的如此吗?运行结果为:
1 2 3 4 5 6 |
method1 get the lock method1 release the lock // sleep了5s method1 costs 5001 method2 get the lock method2 release the lock method2 costs 1 |
Thread1首先执行,睡眠了5s后,Thread1执行完毕,Thread2才开始执行。很明显,两条线程没有实现并发,这就推翻了之前的结论。
几个不同线程同时访问某个类的同一个synchronized方法时, 当一个线程执行时, 其他线程都等待; 若是几个不同线程访问的是一个类不同的synchronized, 则可以同时执行。
现在我们把method2的synchronized修饰删掉,改造成非同步方法,看看会发生什么。
1 2 3 4 5 6 7 8 9 |
public void method2() { long start = System.currentTimeMillis(); System.out.println("method2 get the lock"); System.out.println("method2 release the lock"); System.out.println("method2 costs " + (System.currentTimeMillis() - start)); } |
运行结果为:
1 2 3 4 5 6 |
method1 get the lock method2 get the lock method2 release the lock method2 costs 1 method1 release the lock method1 costs 5001 |
现在这两条线程可以实现并发了。借用别人总结的结论:
多个线程访问同一个类的synchronized方法时, 都是串行执行的(就算有多个cpu也不例外)!synchronized方法使用了类的内置锁, 锁住的是方法所属对象本身。同一个锁某个时刻只能被一个执行线程所获取, 因此其他线程都得等待锁的释放. 因此就算你有多余的cpu可以执行, 但是你没有锁, 所以你还是不能进入synchronized方法执行。
如果Thread1要访问synchronized method1,就要获取test对象的对象锁(method1所属对象),一但Thread1获得了锁,在主动释放之前,(竞争对象锁的)别的线程都需要等待。
现在Thread2想要访问method2,就要先获得test对象的对象锁。但是对象锁已经被Thread1占用了,所以Thread2只能进入test对象的锁池内,等待锁的释放。
顺带一提,Thread.sleep方法不会释放对象锁,这也是和Object.wait方法最大的区别之一(wait不会释放对象锁),我们稍微改写一下,让Thread1进入等待状态,看看能不能释放test的对象锁。
顺顺带一提,wait和notify方法只能写在synchronized修饰的同步方法内,原因是:“如果你没有获得锁,那释放个p啊”、“你连锁都没有获得,你凭什么通知别的线程去获得锁的使用权”。
Test.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
package com.test.testSynchronized; public class Test { public synchronized void method1(Test test) { long start = System.currentTimeMillis(); System.out.println("method1 get the lock"); try { test.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("method1 release the lock"); System.out.println("method1 costs " + (System.currentTimeMillis() - start)); } public synchronized void method2() { long start = System.currentTimeMillis(); System.out.println("method2 get the lock"); System.out.println("method2 release the lock"); System.out.println("method2 costs " + (System.currentTimeMillis() - start)); } public static void main(String[] args) throws InterruptedException { Test test = new Test(); Thread1 thread1 = new Thread1(test); Thread2 thread2 = new Thread2(test); thread1.start(); // 先执行, 以便抢占锁 Thread.sleep(500); // 放弃cpu, 让thread1执行, 以便获的锁 thread2.start(); } } class Thread1 extends Thread { Test test; public Thread1(Test test) { this.test = test; } @Override public void run() { test.method1(test); } } class Thread2 extends Thread { Test test; public Thread2(Test test) { this.test = test; } @Override public void run() { test.method2(); } } |
我猜测:如果test.wait能够释放test的对象锁,那么Thread1将进入等待状态,Thread2获得对象锁后,马上执行method2方法。此时程序并不会退出,因为Thread1还在wait状态,等待唤醒。
运行结果为:
1 2 3 4 |
method1 get the lock method2 get the lock method2 release the lock method2 costs 1 |
结果证实了我的猜想。
顺顺顺带一提,test对象调用了wait方法后,当前线程(Thread1)将进入等待池。“锁池”和“等待池”是java中的重要概念,以后有机会将详细解读。
二、优化同步机制
既然同一对象的不同synchronized方法不是无法同时被多个线程访问,那么现在问题来了:如果method1和method2之间没有同步问题,应该允许同时访问,怎么办?
我们尝试把锁分解一下:
Test.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
package com.test.testSynchronized; public class Test { private Object lock1 = new Object(); private Object lock2 = new Object(); public void method1(Test test) { synchronized (lock1) { long start = System.currentTimeMillis(); System.out.println("method1 get the lock"); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("method1 release the lock"); System.out.println("method1 costs " + (System.currentTimeMillis() - start)); } } public void method2() { synchronized (lock2) { long start = System.currentTimeMillis(); System.out.println("method2 get the lock"); System.out.println("method2 release the lock"); System.out.println("method2 costs " + (System.currentTimeMillis() - start)); } } public static void main(String[] args) throws InterruptedException { Test test = new Test(); Thread1 thread1 = new Thread1(test); Thread2 thread2 = new Thread2(test); thread1.start(); // 先执行, 以便抢占锁 Thread.sleep(500); // 放弃cpu, 让thread1执行, 以便获的锁 thread2.start(); } } class Thread1 extends Thread { Test test; public Thread1(Test test) { this.test = test; } @Override public void run() { test.method1(test); } } class Thread2 extends Thread { Test test; public Thread2(Test test) { this.test = test; } @Override public void run() { test.method2(); } } |
新建两个对象lock1、lock2,使用synchronized代码块代替synchronized方法。现在method1获得的是lock1的对象锁,lock2获得的是lock2的对象锁,这两个方法的冲突就被分解了。运行结果为:
1 2 3 4 5 6 |
method1 get the lock method2 get the lock method2 release the lock method2 costs 1 method1 release the lock method1 costs 5000 |
这里还可以再优化一下,如果method1中的耗时操作是线程安全的(比如输出一个文件?),可以将此类代码从synchronized代码块中移出,减少占用对象锁的时间。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public void method1(Test test) { long start = System.currentTimeMillis(); synchronized (lock1) { System.out.println("method1 get the lock"); System.out.println("method1 release the lock"); } try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("method1 costs " + (System.currentTimeMillis() - start)); } |
实际上,看到这种利用lock1、lock2对象锁的方法,有没有想起java中的另一种同步方式?我们用Lock改写一下:
Test.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
package com.test.testSynchronized; import java.util.concurrent.locks.ReentrantLock; public class Test { private ReentrantLock lock1 = new ReentrantLock(); private ReentrantLock lock2 = new ReentrantLock(); public void method1(Test test) { long start = System.currentTimeMillis(); try { lock1.lock(); System.out.println("method1 get the lock"); } finally { lock1.unlock(); } try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("method1 release the lock"); System.out.println("method1 costs " + (System.currentTimeMillis() - start)); } public void method2() { long start = System.currentTimeMillis(); try { lock2.lock(); System.out.println("method2 get the lock"); System.out.println("method2 release the lock"); } finally { lock2.unlock(); } System.out.println("method2 costs " + (System.currentTimeMillis() - start)); } public static void main(String[] args) throws InterruptedException { Test test = new Test(); Thread1 thread1 = new Thread1(test); Thread2 thread2 = new Thread2(test); thread1.start(); // 先执行, 以便抢占锁 Thread.sleep(500); // 放弃cpu, 让thread1执行, 以便获的锁 thread2.start(); } } class Thread1 extends Thread { Test test; public Thread1(Test test) { this.test = test; } @Override public void run() { test.method1(test); } } class Thread2 extends Thread { Test test; public Thread2(Test test) { this.test = test; } @Override public void run() { test.method2(); } } |
运行结果一模一样:
1 2 3 4 5 6 |
method1 get the lock method2 get the lock method2 release the lock method2 costs 1 method1 release the lock method1 costs 5024 |
三、总结
还是自己敲敲代码理解得比较好。