Groovy死锁例子
import java.util.concurrent.atomic.AtomicInteger
class DeadLock{
static AtomicInteger lock = new AtomicInteger()
static AtomicInteger lock2 = new AtomicInteger()
static message1(){
//If no thread obtains the lock, then execute
synchronized(lock){
println "I am" + Thread.currentThread().getName() + ", I am printing a message"
message2()
}
}
static message2(){
synchronized(lock2){
println "I am" + Thread.currentThread().getName() + ", I am printing a message"
message1()
}
}
static void main(String... args) {
Thread.start{
message1()
}
Thread.start{
message2()
}
}
}
1. 运行时用这个参数-Dcom.sun.management.jmxremote
2. 用JConsole查看锁情况
Thread 1
Thread 2