日期:2014-05-20  浏览次数:20688 次

重入锁ReentrantLock() 创建报错 怎么回事??
Java code

import java.util.concurrent.locks.*;

public class ReentrantLock  implements Runnable{
    private int num = 0;
    private Lock lock = new ReentrantLock();//创建重入锁对象。这里怎么报错啊???
    @Override
    public void run(){
        lock.lock();    //打开锁
        try{
            for(int i = 0; i < 5; i++){
                try{
                    Thread.sleep(100);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ":" + num++);
            }
        }finally{
            lock.unlock();//释放锁
        }
    }

    public static void main(String[] args) {
        ReentrantLock run = new ReentrantLock();
        Thread thread1 = new Thread(run);
        Thread thread2 = new Thread(run);
        thread1.start();
        thread2.start();

    }

}



private Lock lock = new ReentrantLock(); 这句 new ReentrantLock() 为什么不能通过编译??



------解决方案--------------------
ReentrantLock 都被你覆盖了,还能是lock吗?
改下名字
public class ReentrantLockRunnable implements Runnable{
private int num = 0;
private Lock lock = new ReentrantLock();//创建重入锁对象。这里怎么报错啊???
即使成功了,也会栈溢出。