您的位置:首页 > 编程语言 > Java开发

java多线程死锁问题

2015-07-19 17:22 471 查看
1、什么是死锁

2、写一个死锁

public class Demo10 {

/**
* 写一个死锁
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread threada = new Thread(new DeadTest(true));
Thread threadb = new Thread(new DeadTest(false));
threada.start();
threadb.start();}

}
class DeadTest implements Runnable{
boolean flag;
DeadTest(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 locka");
}
}
}
}
}
class Mylock{
static Object locka = new Object();
static Object lockb = new Object();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: