代码改变世界

Groovy死锁例子

2011-12-14 08:32  康杜  阅读(716)  评论(0编辑  收藏  举报

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

Image(1)

2. 用JConsole查看锁情况

Image(2)

Thread 1

Image(3)

Thread 2

Image(4)