public class test {
object leftlock = new object();
object rightlock = new object();
public static void main(string[] args) {
final test test = new test();
thread a = new thread(new runnable() {
@override public void run() {
int i=0;
while (i<10)
{
test.leftright();
i++;
}
}
},"athread");
thread b = new thread(new runnable() {
@override public void run() {
int i=0;
while (i<10)
{
test.rightleft();
i++;
}
}
},"bthread");
a.start();
b.start();
}
public void leftright(){
synchronized (leftlock){
system.out.println(thread.currentthread().getname()+":leftright:get left");
synchronized (rightlock){
system.out.println(thread.currentthread().getname()+":leftright:get right");
}
}
}
public void rightleft(){
synchronized (rightlock){
system.out.println(thread.currentthread().getname()+":rightleft: get right");
synchronized (leftlock){
system.out.println(thread.currentthread().getname()+":rightleft: get left");
}
}
}
}
运行后输出如下
athread:leftright:get left
bthread:rightleft: get right
可以通过jstack发现死锁的痕迹
"bthread" prio=5 tid=0x00007fabb2001000 nid=0x5503 waiting for monitor entry [0x000000011d54b000]
java.lang.thread.state: blocked (on object monitor)
at main.locktest.test.rightleft(test.java:52)
- waiting to lock <0x00000007aaee5748> (a java.lang.object)
- locked <0x00000007aaee5758> (a java.lang.object)
at main.locktest.test$2.run(test.java:30)
at java.lang.thread.run(thread.java:745)
locked ownable synchronizers:
- none
"athread" prio=5 tid=0x00007fabb2801000 nid=0x5303 waiting for monitor entry [0x000000011d448000]
java.lang.thread.state: blocked (on object monitor)
at main.locktest.test.leftright(test.java:43)
- waiting to lock <0x00000007aaee5758> (a java.lang.object)
- locked <0x00000007aaee5748> (a java.lang.object)
at main.locktest.test$1.run(test.java:19)
at java.lang.thread.run(thread.java:745)
locked ownable synchronizers:
- none
public class executorlock {
private static executorservice single=executors.newsinglethreadexecutor();
public static class anothercallable implements callable<string>{
@override public string call() throws exception {
system.out.println("in anothercallable");
return "annother success";
}
}
public static class mycallable implements callable<string>{
@override public string call() throws exception {
system.out.println("in mycallable");
future<string> submit = single.submit(new anothercallable());
return "success:"+submit.get();
}
}
public static void main(string[] args) throws executionexception, interruptedexception {
mycallable task = new mycallable();
future<string> submit = single.submit(task);
system.out.println(submit.get());
system.out.println("over");
single.shutdown();
}
}
执行的输出只有一行
in mycallable
通过jstack观察可以看到如下
"main" prio=5 tid=0x00007fab3f000000 nid=0x1303 waiting on condition [0x0000000107d63000]
java.lang.thread.state: waiting (parking)
at sun.misc.unsafe.park(native method)
- parking to wait for <0x00000007aaeed1d8> (a java.util.concurrent.futuretask)
at java.util.concurrent.locks.locksupport.park(locksupport.java:186)
at java.util.concurrent.futuretask.awaitdone(futuretask.java:425)
at java.util.concurrent.futuretask.get(futuretask.java:187)
at main.locktest.executorlock.main(executorlock.java:32)
at sun.reflect.nativemethodaccessorimpl.invoke0(native method)
at sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57)
at sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)
at java.lang.reflect.method.invoke(method.java:606)
at com.intellij.rt.execution.application.appmain.main(appmain.java:140)
locked ownable synchronizers:
- none
..
"pool-1-thread-1" prio=5 tid=0x00007fab3f835800 nid=0x5303 waiting on condition [0x00000001199ee000]
java.lang.thread.state: waiting (parking)
at sun.misc.unsafe.park(native method)
- parking to wait for <0x00000007ab0f8698> (a java.util.concurrent.futuretask)
at java.util.concurrent.locks.locksupport.park(locksupport.java:186)
at java.util.concurrent.futuretask.awaitdone(futuretask.java:425)
at java.util.concurrent.futuretask.get(futuretask.java:187)
at main.locktest.executorlock$mycallable.call(executorlock.java:26)
at main.locktest.executorlock$mycallable.call(executorlock.java:20)
at java.util.concurrent.futuretask.run(futuretask.java:262)
at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1145)
at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:615)
at java.lang.thread.run(thread.java:745)
locked ownable synchronizers:
- <0x00000007aaeed258> (a java.util.concurrent.threadpoolexecutor$worker)