Java多线程ReentrantLock问题(阿里云一面笔试)

Java多线程ReentrantLock问题(阿里云一面笔试)

今天做了一道面试题,题目要求大概是创建三个线程,A线程打印A,B线程打印B,C线程打印C,并且一直按这个顺序打印。要求线程启动顺序为C->B->A,且不能用sleep函数。

有趣的地方是该题使我对线程start的理解更深了,因为之前的并发情况大部分处理的是卖票并发,不考虑线程轮转拿锁公平问题。首先,考虑到并发性和严格轮询(目前暂时不考虑CBA,我们先只按ABC进行输出)我的直接印象是上公平锁:new ReentrantLock(true)。代码如下:

public class Test{
    
    public static void main(String[] args) throws Exception{        
        System.out.println();
        Task task = new Task();
        Thread1 thread1 = new Thread1("A", task);
        Thread2 thread2 = new Thread2("B", task);
        Thread3 thread3 = new Thread3("C", task);

        thread1.start();
        thread2.start();
        thread3.start();
    }
}


class Task implements Runnable{
    private ReentrantLock lock = new ReentrantLock(true);
    private int index = 0; // 避免线程一直跑

    @Override
    public void run() {
            while (index < 60){
                lock.lock();
                try{
                    System.out.print(Thread.currentThread().getName());
                    index++;
                    // Thread.sleep(1);
                    // Thread.sleep(10);
                }catch (Exception e){
                    e.printStackTrace();
                }finally{
                    lock.unlock();
                }
            }
    }
}


class Thread1 extends Thread{
    private Runnable task;
    public Thread1(String name, Runnable task){
        super(name);
        this.task = task;
    }

    @Override
    public void run() {
        task.run();
    }
}



class Thread2 extends Thread{
    private Runnable task;
    public Thread2(String name, Runnable task){
        super(name);
        this.task = task;
    }

    @Override
    public void run() {
        task.run();
    }
}


class Thread3 extends Thread{
    private Runnable task;
    public Thread3(String name, Runnable task){
        super(name);
        this.task = task;
    }

    @Override
    public void run() {
        task.run();
    }
}
  • 错误的现象发生了,输出并不是想象中的ABCABCABC…
    结果是:AAAAAAAAAAAAAAAAABABABABABACBACBACBACBACBACBACBACBACBACBABACBA

  • 但是如果在// Thread.sleep(1);注释处加这一条sleep,
    结果是:ABACBACBACBACBACBACBACBACBACBACBACBACBACBACBACBACBACBACBACBAC

  • 如果把sleep时间加长Thread.sleep(10)
    结果是:
    ABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCABCAB
    便符合严格轮询要求

通过查询资料,发现原来是因为:

  • 原因1:
    线程调用start()之后并不是立刻开始执行run方法。他需要等待其他系统资源准备就绪。例如:CPU。
    Thread0最开始可能没有获取到CPU时间,Thread1却获取到且跑了几次。

    参考:https://ask.csdn.net/questions/701183?spm=1005.2026.3001.5635评论第一条
    但这种情况在我们的输出结果中并没有体现

  • 原因2:
    我认为,出现前两种错误情况还是因为main也就是主线程还没执行到thread2.start()(也就是thread2还没有拿到锁,自然无法实现公平的功能),此时thread1已经执行了不少轮了。

通过以上分析,再加上题中规定线程启动顺序为C->B->A,且不能用sleep函数,所以该题需要用个变量记录或是用Condition来解决。
解决代码如下:

public class Test{
    public static Lock lock = new ReentrantLock();

    public static Condition conditionA = lock.newCondition();
    public static Condition conditionB = lock.newCondition();
    public static Condition conditionC = lock.newCondition();

    public static int index = 0;


    public static void main(String[] args) throws Exception{        
        System.out.println();

        Thread1 thread1 = new Thread1();
        Thread2 thread2 = new Thread2();
        Thread3 thread3 = new Thread3();

        thread3.start();
        thread2.start();
        thread1.start();
    }
}



class Thread1 extends Thread{
    @Override
    public void run() {
        while (Test.index < 60){
            
            Test.lock.lock();
            try {
                if (Test.index % 3 != 0){
                    Test.conditionA.await();
                }
                System.out.print("A");
                Test.index++;
                Test.conditionB.signal();
                
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally{
                Test.lock.unlock();
            }
            
        }
    }
}


class Thread2 extends Thread{
    @Override
    public void run() {
        while (Test.index < 60){
            
            Test.lock.lock();
            try {
                if (Test.index % 3 != 1){
                    Test.conditionB.await();
                }
                System.out.print("B");
                Test.index++;
                Test.conditionC.signal();
                
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally{
                Test.lock.unlock();
            }
        }
    }
}


class Thread3 extends Thread{
    @Override
    public void run() {
        while (Test.index < 60){
            
            Test.lock.lock();
            try {
                if (Test.index % 3 != 2){
                    Test.conditionC.await();
                }
                System.out.print("C");
                Test.index++;
                Test.conditionA.signal();
                
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally{
                Test.lock.unlock();
            }
            
        }
    }
}

总结:更加深了对java中锁的理解,synchronized和ReentrantLock(false)对应c/c++中的pthread_mutex_lock互斥锁,Condition对应c/c++中的条件变量(因为java中Condition也是挂在lock上的,但需要注意,这里的condition更灵活,因为可以唤醒指定的condition)。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值