当前位置: 技术问答>java相关
关于死锁的一个程序,有几个地方不明白,请教一下
来源: 互联网 发布时间:2015-03-09
本文导语: public class Deadlock implements Runnable { public static void main(String[] args) { Object a = "Resource A"; Object b = "Resource B"; Thread t1 = new Thread(new Deadlock(a, b)); Thread t2 = new Thread(new Deadlock(b,...
public class Deadlock implements Runnable
{
public static void main(String[] args)
{
Object a = "Resource A";
Object b = "Resource B";
Thread t1 = new Thread(new Deadlock(a, b));
Thread t2 = new Thread(new Deadlock(b, a));
t1.start();
t2.start();
}
private Object firstResource;
private Object secondResource;
public Deadlock(Object first, Object second)
{
firstResource = first;
secondResource = second;
}
public void run()
{
for (;;)
{
System.out.println(Thread.currentThread().getName()+" Looking for lock on "+firstResource);
synchronized (firstResource)
{
System.out.println(Thread.currentThread().getName()+" Obtained lock on "+firstResource);
System.out.println(Thread.currentThread().getName()+" Looking for lock on " + secondResource);
synchronized (secondResource)
{
System.out.println(Thread.currentThread().getName()+" Obtained lock on "+secondResource);
// simulate some time consuming activity
try
{
Thread.sleep(100);
}
catch (InterruptedException ex) {}
}
}
}
}
}
输出结果:
Thread-0 Looking for lock on Resource A
Thread-0 Obtained lock on Resource A
Thread-0 Looking for lock on Resource B
Thread-0 Obtained lock on Resource B
Thread-1 Looking for lock on Resource B
Thread-1 Obtained lock on Resource B
Thread-1 Looking for lock on Resource A
Thread-1 Obtained lock on Resource A
Thread-0 Looking for lock on Resource A
Thread-1 Looking for lock on Resource B
Thread-0 Obtained lock on Resource A
Thread-0 Looking for lock on Resource B
Thread-1 Obtained lock on Resource B
Thread-1 Looking for lock on Resource A
(死锁中......)
上面这个程序的死锁原理我是知道的,就是线程0获得了对象A的锁,在等待获得对象B的锁;而线程B刚好相反。所以造成了死锁。但是看这个程序后有几个问题不懂,请教一下各位:
1.synchronized (对象 a) { } 这样的写法是不是将这个对象a锁定,只能同时让一个线程使用?
2.输出结果为什么是这样的?能不能把输出结果一行一行的解释来听听?
{
public static void main(String[] args)
{
Object a = "Resource A";
Object b = "Resource B";
Thread t1 = new Thread(new Deadlock(a, b));
Thread t2 = new Thread(new Deadlock(b, a));
t1.start();
t2.start();
}
private Object firstResource;
private Object secondResource;
public Deadlock(Object first, Object second)
{
firstResource = first;
secondResource = second;
}
public void run()
{
for (;;)
{
System.out.println(Thread.currentThread().getName()+" Looking for lock on "+firstResource);
synchronized (firstResource)
{
System.out.println(Thread.currentThread().getName()+" Obtained lock on "+firstResource);
System.out.println(Thread.currentThread().getName()+" Looking for lock on " + secondResource);
synchronized (secondResource)
{
System.out.println(Thread.currentThread().getName()+" Obtained lock on "+secondResource);
// simulate some time consuming activity
try
{
Thread.sleep(100);
}
catch (InterruptedException ex) {}
}
}
}
}
}
输出结果:
Thread-0 Looking for lock on Resource A
Thread-0 Obtained lock on Resource A
Thread-0 Looking for lock on Resource B
Thread-0 Obtained lock on Resource B
Thread-1 Looking for lock on Resource B
Thread-1 Obtained lock on Resource B
Thread-1 Looking for lock on Resource A
Thread-1 Obtained lock on Resource A
Thread-0 Looking for lock on Resource A
Thread-1 Looking for lock on Resource B
Thread-0 Obtained lock on Resource A
Thread-0 Looking for lock on Resource B
Thread-1 Obtained lock on Resource B
Thread-1 Looking for lock on Resource A
(死锁中......)
上面这个程序的死锁原理我是知道的,就是线程0获得了对象A的锁,在等待获得对象B的锁;而线程B刚好相反。所以造成了死锁。但是看这个程序后有几个问题不懂,请教一下各位:
1.synchronized (对象 a) { } 这样的写法是不是将这个对象a锁定,只能同时让一个线程使用?
2.输出结果为什么是这样的?能不能把输出结果一行一行的解释来听听?
|
that paragraph is got from the JDK's document. and the book u mentioned I did not read ever.
ur understanding is right i think.
the staus of a thread can be one of these:new,runnable(ready u said),running,dead,blocked. a thread be running must be runnable first just as u said above, for there a thread pool and JVM will has it's own arithmetic to pick which runnable thread actually run from the pool. but we can omit this step if we no need care much about the time for make the thread from runnable to running. so there's no confliction between what i said and ur words.
ur understanding is right i think.
the staus of a thread can be one of these:new,runnable(ready u said),running,dead,blocked. a thread be running must be runnable first just as u said above, for there a thread pool and JVM will has it's own arithmetic to pick which runnable thread actually run from the pool. but we can omit this step if we no need care much about the time for make the thread from runnable to running. so there's no confliction between what i said and ur words.
|
我把sleep的时机改了一下我想有助于你理解。其实以上的代码发生死锁的概率要比下面的代码小一些而以
public class Deadlock implements Runnable
{
public static void main(String[] args)
{
Object a = "Resource A";
Object b = "Resource B";
Thread t1 = new Thread(new Deadlock(a, b));
Thread t2 = new Thread(new Deadlock(b, a));
t1.start();
t2.start();
}
private Object firstResource;
private Object secondResource;
public Deadlock(Object first, Object second)
{
firstResource = first;
secondResource = second;
}
public void run()
{
for (;;)
{
System.out.println(Thread.currentThread().getName()+" Looking for lock on "+firstResource);
synchronized (firstResource)
{
System.out.println(Thread.currentThread().getName()+" Obtained lock on "+firstResource);
try
{
Thread.sleep(1000);
}
catch (InterruptedException ex) {}
System.out.println(Thread.currentThread().getName()+" Looking for lock on " + secondResource);
synchronized (secondResource)
{
System.out.println(Thread.currentThread().getName()+" Obtained lock on "+secondResource);
// simulate some time consuming activity
}
}
}
}
}
public class Deadlock implements Runnable
{
public static void main(String[] args)
{
Object a = "Resource A";
Object b = "Resource B";
Thread t1 = new Thread(new Deadlock(a, b));
Thread t2 = new Thread(new Deadlock(b, a));
t1.start();
t2.start();
}
private Object firstResource;
private Object secondResource;
public Deadlock(Object first, Object second)
{
firstResource = first;
secondResource = second;
}
public void run()
{
for (;;)
{
System.out.println(Thread.currentThread().getName()+" Looking for lock on "+firstResource);
synchronized (firstResource)
{
System.out.println(Thread.currentThread().getName()+" Obtained lock on "+firstResource);
try
{
Thread.sleep(1000);
}
catch (InterruptedException ex) {}
System.out.println(Thread.currentThread().getName()+" Looking for lock on " + secondResource);
synchronized (secondResource)
{
System.out.println(Thread.currentThread().getName()+" Obtained lock on "+secondResource);
// simulate some time consuming activity
}
}
}
}
}