面试题:两个线程交替打印、三个线程交替打印、拓展:N个线程交替打印

           目录 

利用condition等待队列实现两个线程的交替打印

利用condition等待队列和计数count实现三个线程的交替打印

拓展:N个线程交替打印


利用condition等待队列实现两个线程的交替打印

代码如下


import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class LockDemo {
    //定义全局锁
    private static ReentrantLock lock = new ReentrantLock();
    private static Condition condition1 = lock.newCondition();
    private static Condition condition2 = lock.newCondition();
    public static void main(String[] args) {
        Thread thread1 = new Thread(new S());
        Thread thread2 = new Thread(new T());
        thread1.setPriority(Thread.MAX_PRIORITY);
        thread1.start();
        thread2.start();
    }


    static class S implements Runnable{
        @Override
        public void run() {
            for (int i = 0 ; i < 10 ; i ++){
                try {
                    lock.lock();
                    System.out.println(Thread.currentThread().getName());
                    condition1.await();
                    condition2.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }


        }
    }




    static class T implements Runnable{
        @Override
        public void run() {
            for (int i = 0 ; i < 10 ; i ++){
                try {
                    lock.lock();
                    System.out.println(Thread.currentThread().getName());
                    condition1.signal();
                    condition2.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        }
    }
}

利用condition等待队列和计数count实现三个线程的交替打印

代码如下

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;


public class LockDemo {
    private static int count = 0;
    //定义全局锁
    private static ReentrantLock lock = new ReentrantLock();
    private static Condition condition1 = lock.newCondition();
    private static Condition condition2 = lock.newCondition();
    private static Condition condition3 = lock.newCondition();
    public static void main(String[] args) {
        Thread thread1 = new Thread(new Thread1());
        Thread thread2 = new Thread(new Thread2());
        Thread thread3 = new Thread(new Thread3());
        thread1.setPriority(Thread.MAX_PRIORITY);
        thread1.start();
        thread2.start();
        thread3.start();
    }


    static class Thread1 implements Runnable{
        @Override
        public void run() {
            for (int i = 0 ; i < 10 ; i ++){
                try {
                    lock.lock();
                    if (count % 3 != 0)
                        condition1.await();
                    System.out.println(i+"***A");
                    count++;
                    condition2.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        }
    }

    static class Thread2 implements Runnable{
        @Override
        public void run() {
            for (int i = 0 ; i < 10 ; i ++){
                try {
                    lock.lock();
                    if (count % 3 != 1)
                        condition2.await();
                    System.out.println(i+"***B");
                    count++;
                    condition3.signal();// 唤醒条件3
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        }
    }

    static class Thread3 implements Runnable{
        @Override
        public void run() {
            for (int i = 0 ; i < 10 ; i ++){
                try {
                    lock.lock();
                    if (count % 3 != 2)
                        condition3.await();
                    System.out.println(i+"***C");
                    count++;
                    condition1.signal();// 唤醒条件1
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        }
    }

}

拓展:N个线程交替打印

参考以上三个线程交替打印的方案。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值