标题:求助:关于ReentrantLock的lock的问题
只看楼主
我是戴寒
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-7-3
 问题点数:0 回复次数:1 
求助:关于ReentrantLock的lock的问题
先看下面这个程序
public class App {
    static class NumberWrapper {
        public int value = 1;
    }

    public static void main(String[] args) {
        //初始化可重入锁
        final ReentrantLock lock = new ReentrantLock();
        
        //第一个条件当屏幕上输出到3
        final Condition reachThreeCondition = lock.newCondition();
        //第二个条件当屏幕上输出到6
        final Condition reachSixCondition = lock.newCondition();
        
        //NumberWrapper只是为了封装一个数字,一边可以将数字对象共享,并可以设置为final
        //注意这里不要用Integer, Integer 是不可变对象
        final NumberWrapper num = new NumberWrapper();
        //初始化A线程
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                //需要先获得锁
                lock.lock();
                try {
                    System.out.println("threadA start write1");
                    System.out.println("ThreadA:"+lock.getHoldCount()+"次");
                    //A线程先输出前3个数
                    while (num.value <= 3) {
                        System.out.println(num.value);
                        num.value++;
                    }
                    //输出到3时要signal,告诉B线程可以开始了
                    reachThreeCondition.signal();
                } finally {
                    lock.unlock();
                }
                lock.lock();
                try {
                    //等待输出6的条件
                    reachSixCondition.await();
                    System.out.println("threadA start write2");
                    //输出剩余数字
                    while (num.value <= 9) {
                        System.out.println(num.value);
                        num.value++;
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
            }

        });


        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock.lock();
                    System.out.println("threadB aquare lock");
                    System.out.println("ThreadB:"+lock.getHoldCount()+"次");
                    while (num.value <= 3) {
                        //等待3输出完毕的信号
                        reachThreeCondition.await();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
                try {
                    lock.lock();
                    //已经收到信号,开始输出4,5,6
                    System.out.println("threadB start write");
                    while (num.value <= 6) {
                        System.out.println(num.value);
                        num.value++;
                    }
                    //4,5,6输出完毕,告诉A线程6输出完了
                    reachSixCondition.signal();
                } finally {
                    lock.unlock();
                }
            }

        });


        //启动两个线程
        threadB.start();
        threadA.start();
    }
}

第一个问题:两个线程A和B都进入了各自的第一个lock方法里??为什么能进入,不是应该等待另一个释放锁吗?毕竟用的是同一个Reentrantlock
第二个问题:既然都进入了,但是他们都进入后,打印出的count都为1??我理解的是一个为1,一个2,毕竟用的是同一个Reentrantlock,,

谢谢了,
搜索更多相关主题的帖子: public 
2016-05-06 08:09
我是戴寒
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2015-7-3
得分:0 
好吧,,是因为B线程调用await()释放锁,所以感觉好像是都获得锁了,实际上还是B释放锁,A获得锁
2016-05-06 11:41



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-464451-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.065081 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved