來模擬一個死鎖(互相等待):
TestDeadLock.java
package com.zhj.www;public class TestDeadLock implements Runnable {public int flag = 1;static Object o1 = new Object();static Object o2 = new Object();public void run() {System.out.println("flag=" + flag);if(flag ==1) {synchronized (o1) {try {Thread.sleep(500);}catch (Exception e) {e.getStackTrace();}synchronized (o2) {System.out.println("1");}}}if(flag ==0) {synchronized (o2) {try {Thread.sleep(500);}catch (Exception e) {e.getStackTrace();}synchronized (o1) {System.out.println("0");}}}}public static void main(String[] args) {TestDeadLock td1 = new TestDeadLock();TestDeadLock td2 = new TestDeadLock();td1.flag = 1;td2.flag = 0;Thread t1 = new Thread(td1);Thread t2 = new Thread(td2);t1.start();t2.start();}}
運行結果:
我們發現程序處于已知等待的情況下。
哲學家吃飯問題(多個線程死鎖):
怎么解決?把鎖的粒度加粗,不如把整個對象鎖住,而不是只是鎖幾行。
粒度:實現數據庫的產品,鎖在行一級還是表一級。
面試題:
package com.zhj.www;
public class TestInterviewQuestion1 implements Runnable {int b =100;public synchronized void m1() {try {b = 1000;Thread.sleep(5000);System.out.println("b = "+b);} catch (Exception e) {e.getStackTrace();}}public void m2() {System.out.println(b);}public void run() {try {m1();}catch (Exception e) {e.getStackTrace();}}public static void main(String[] args) throws Exception{TestInterviewQuestion1 testInterviewQuestion1 = new TestInterviewQuestion1();Thread thread = new Thread(testInterviewQuestion1);thread.start();Thread.sleep(1000);testInterviewQuestion1.m2();}
}
運行結果:
1000
b = 1000
修改一下:
package com.zhj.www;public class TestInterviewQuestion1 implements Runnable {int b =100;public synchronized void m1() {try {b = 1000;Thread.sleep(5000);System.out.println("b = "+b);} catch (Exception e) {e.getStackTrace();}}public synchronized void m2() throws Exception {Thread.sleep(2500);b = 2000;}public void run() {try {m1();}catch (Exception e) {e.getStackTrace();}}public static void main(String[] args) throws Exception{TestInterviewQuestion1 testInterviewQuestion1 = new TestInterviewQuestion1();Thread thread = new Thread(testInterviewQuestion1);thread.start();//Thread.sleep(1000);testInterviewQuestion1.m2();System.out.println(testInterviewQuestion1.b);}
}
對每個方法加不加鎖,要考慮清楚。
互斥:某個時間段,只有一個線程進入這個方法,但不保證不進入另外一個線程。
不允許多個線程同時寫,但允許多個線程讀。