来模拟一个死锁(互相等待):
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);}
}
对每个方法加不加锁,要考虑清楚。
互斥:某个时间段,只有一个线程进入这个方法,但不保证不进入另外一个线程。
不允许多个线程同时写,但允许多个线程读。