package process;
/*
*死锁:
*同步中嵌套同步,并且锁不同
*/
class Ticket5 implements Runnable{
public static int tick = 1000;
Object obj = new Object();
boolean flag = true;
int i = 0;
@Override
public void run() {
if(flag){
while(true){
synchronized(obj){
show();
}
}
}else{
show();
}
}
public synchronized void show(){
synchronized(obj){
if(tick > 0){
System.out.println(Thread.currentThread().getName() + "sale: " + tick--);
}
}
}
}
public class DeadLockDemo {
public static void main(String[] args){
Tickets t = new Tickets();
Thread t1 = new Thread(t);
Thread t2 = new Thread(t);
t1.start();
t2.start();
}
}
Thread(多线程 死锁 同步中嵌套同步,并且锁不同)
最新推荐文章于 2024-04-08 22:29:04 发布