public class DeadLock {
private static Object locka = new Object();
private static Object lockb = new Object();
public static void main(String[] args){
new DeadLock().deadLock();
}
private void deadLock(){
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (locka){
try{
System.out.println(Thread.currentThread().getName()+" get locka ing!");
Thread.sleep(500);
System.out.println(Thread.currentThread().getName()+" after sleep 500ms!");
}catch(Exception e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" need lockb!Just waiting!");
synchronized (lockb){
System.out.println(Thread.currentThread().getName()+" get lockb ing!");
}
}
}
},"thread1");
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lockb){
try{
System.out.println(Thread.currentThread().getName()+" get lockb ing!");
Thread.sleep(500);
System.out.println(Thread.currentThread().getName()+" after sleep 500ms!");
}catch(Exception e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" need locka! Just waiting!");
synchronized (locka){
System.out.println(Thread.currentThread().getName()+" get locka ing!");
}
}
}
},"thread2");
thread1.start();
thread2.start();
}
}
java 死锁 简单实例
最新推荐文章于 2024-03-19 13:38:45 发布