请各位帮忙看看,以下是代码        static ReentrantLock lock = new ReentrantLock();

public static void main(String[] args) {
ExecutorService es = Executors.newCachedThreadPool();

es.execute(new Runnable() {

@Override
public void run() {
foo("thread1");
}
});

es.execute(new Runnable() {

@Override
public void run() {
foo("thread2");
}
});
}

private static void foo(String threadName) {
try {
lock.tryLock();
System.out.println(lock.getHoldCount());
System.out.println(threadName);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} finally {
lock.unlock();
System.out.println(lock.getHoldCount());
}
}输出信息是:
1
thread1
(此处无等待)
0
thread2
0
Exception in thread "pool-1-thread-2" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:155)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1260)
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:460)
at test.reentrantlock.Test.foo(Test.java:43)
at test.reentrantlock.Test.access$0(Test.java:31)
at test.reentrantlock.Test$2.run(Test.java:26)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)为什么线程2会立刻获得锁,而且获得锁之后的HoldCount是0,最后抛出的异常是因为线程1准备释放锁时,它已经不是锁持有者了。线程

解决方案 »

  1.   

    你得仔细看看API,你对ReentrantLock理解有问题
    private static void foo(String threadName) {
    if (lock.tryLock()) {//仅在调用时锁为空闲状态才获取该锁。 
                 //如果锁可用,则获取锁,并立即返回值 true。
                 //如果锁不可用,则此方法将立即返回值 false。 System.out.println(lock.getHoldCount());
    System.out.println(threadName);
    try {
    Thread.sleep(2000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    } finally {
    lock.unlock();//释放锁在这,不是下面
    System.out.println(lock.getHoldCount() + "**");
    }
    }
    }
    线程池也没有关闭,es.shutdown();//必须关闭线程池
      

  2.   

    改成这个你再试一下:
    ExecutorService es = Executors.newFixedThreadPool(1);
             
      

  3.   

    public int getHoldCount()
    Queries the number of holds on this lock by the current thread. 
    因为锁是可重入的,同一个线程可多次获取同一把锁。getHoldCount()说的就是线程获取了几次锁了
      

  4.   

    可以试试用lockInterruptibly方法,它会在无法获取锁时阻塞
                    lock.lockInterruptibly();
    System.out.println(lock.getHoldCount());
    System.out.println(threadName);
    try {
    Thread.sleep(2000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    } finally {
    lock.unlock();
    System.out.println(lock.getHoldCount() + "**");
    }
      

  5.   


    import java.util.concurrent.*;
    import java.util.concurrent.locks.*;
    public class ReentrantLockTest{
    static ReentrantLock lock = new ReentrantLock();
         
        public static void main(String[] args) {
            ExecutorService es = Executors.newCachedThreadPool();
             
            es.execute(new Runnable() {
                 
                @Override
                public void run() {
                    foo("thread1");
                }
            });
             
            es.execute(new Runnable() {
                 
                @Override
                public void run() {
                    foo("thread2");
                }
            });
        }
         
        private static void foo(String threadName) {
         while(true){
             if(lock.tryLock()){
             try{
                 System.out.println(Thread.currentThread().getName()+":"+lock.getHoldCount());
                System.out.println(Thread.currentThread().getName()+":"+threadName);
                try {Thread.sleep(2000);} catch (InterruptedException e) { e.printStackTrace();}
                break;
                }finally {
                lock.unlock();
                System.out.println(Thread.currentThread().getName()+":"+lock.getHoldCount());
            }
         } 
         }
        }
    }