/**
* 死锁举例
*/
package java160109;
/**
* @author LiZheng
*
*/
public class DeadLockTest {
/**
* @param args
*/
public static void main(String[] args) {
Thread thread1 =new Thread(new Test(true));
Thread thread2 =new Thread(new Test(false));
thread1.start();
thread2.start();
}
}
class Test implements Runnable{
private boolean flag;
Test(boolean flag) {
this.flag=flag;
}
@Override
public void run() {
if (flag) {
while (true) {
System.out.println("if locka");
synchronized (MyLoack.locka) {
synchronized (MyLoack.lockb) {
System.out.println("if lockb");
}
}
}
} else {
while (true) {
System.out.println("else lockb");
synchronized (MyLoack.lockb) {
synchronized (MyLoack.locka) {
System.out.println("else locka");
}
}
}
}
}
}
class MyLoack{
static Object locka=new Object();
static Object lockb=new Object();
}
* 死锁举例
*/
package java160109;
/**
* @author LiZheng
*
*/
public class DeadLockTest {
/**
* @param args
*/
public static void main(String[] args) {
Thread thread1 =new Thread(new Test(true));
Thread thread2 =new Thread(new Test(false));
thread1.start();
thread2.start();
}
}
class Test implements Runnable{
private boolean flag;
Test(boolean flag) {
this.flag=flag;
}
@Override
public void run() {
if (flag) {
while (true) {
System.out.println("if locka");
synchronized (MyLoack.locka) {
synchronized (MyLoack.lockb) {
System.out.println("if lockb");
}
}
}
} else {
while (true) {
System.out.println("else lockb");
synchronized (MyLoack.lockb) {
synchronized (MyLoack.locka) {
System.out.println("else locka");
}
}
}
}
}
}
class MyLoack{
static Object locka=new Object();
static Object lockb=new Object();
}