死锁。
死锁的原因:同步中嵌套同步,多个进程同时抢到不同的锁,都在等待,互不相让
在该程序中, 假如线程0在run方法中打开obj锁,同时1线程在show方法打开this锁,
此时下一步 ,线程0和线程1都需要双方的锁,故都在等待,从而出现了死锁
class Ticket implements Runnable {
private int tick = 100;
Object obj = new Object();
boolean flag = true;
public void run() {
if (flag) {
while(true) {
synchronized(obj) {//这个obj锁与28行的obj是同一个锁,虽然锁的位置不一样
show();
}
}
}
else
while(true)
this.show();
}
public synchronized void show() { //this锁
synchronized(obj) {
if (tick>0) {
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+"code: "+tick--);
}
}
}
}
class DeadLockDemo {
public static void main(String[] args) {
Ticket t = new Ticket();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
t1.start();
try{Thread.sleep(10);}catch(Exception e){}
t.flag = false;
t2.start();
}
}
练习
if和else中,如果两个线程各自都抢到了一把锁,则死锁,
若某个线程提前抢到了两把锁 则不会出现死锁了
class Test implements Runnable {
private boolean flag;
Test(boolean flag) {
this.flag = flag;
}
public void run() {
if (flag) {
synchronized(MyLock.locka) {
System.out.println("if locka");
synchronized(MyLock.lockb) {
System.out.println("if lockb");
}
}
}
else {
synchronized(MyLock.lockb) {
System.out.println("else lockb");
synchronized(MyLock.locka) {
System.out.println("else lockb");
}
}
}
}
}
class MyLock {
static Object locka = new Object(); //静态后方便直接调用
static Object lockb = new Object();
}
class DeadLockTest {
public static void main(String[] args) {
Thread t1 = new Thread(new Test(true));
Thread t2 = new Thread(new Test(false));
t1.start();
try{Thread.sleep(10);}catch(Exception e){} //加上就不死锁了,不加可能死锁
t2.start();
}
}
网友评论