class Test implements Runnable{
private boolean flag;
int num=100;
Test(boolean flag){
this.flag=flag;
}
@Override
public void run() {
// TODO Auto-generated method stub
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{
public static final Object locka=new Object();
public static final Object lockb=new Object();
}
public class DeadLockDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test a=new Test(true);
Test b=new Test(false);
Thread t1=new Thread(a);
Thread t2=new Thread(b);
t1.start();
t2.start();
}
}
private boolean flag;
int num=100;
Test(boolean flag){
this.flag=flag;
}
@Override
public void run() {
// TODO Auto-generated method stub
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{
public static final Object locka=new Object();
public static final Object lockb=new Object();
}
public class DeadLockDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Test a=new Test(true);
Test b=new Test(false);
Thread t1=new Thread(a);
Thread t2=new Thread(b);
t1.start();
t2.start();
}
}