package com.chnsys.threaddemo;
public class JavaResearch {
/**
* @param args
* 模拟死锁
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Ticket t_true = new Ticket(true);
Ticket t_false = new Ticket(false);
Thread t1 = new Thread(t_true);
Thread t2 = new Thread(t_false);
t1.start();
t2.start();
}
}
class MyLock{
static Object locka = new Object();
static Object lockb = new Object();
}
class Ticket implements Runnable{
public boolean flag;
public Ticket(boolean flag) {
super();
this.flag = flag;
}
@Override
public void run() {
// TODO Auto-generated method stub
if(flag){
while(true){
synchronized (MyLock.locka) {
System.out.println(Thread.currentThread().getName()+" if locka");
synchronized (MyLock.lockb) {
System.out.println(Thread.currentThread().getName()+" if lockb");
}
}
}
}else{
while(true){
synchronized (MyLock.lockb) {
System.out.println(Thread.currentThread().getName()+" else lockb");
synchronized (MyLock.locka) {
System.out.println(Thread.currentThread().getName()+" else locka");
}
}
}
}
}
}