• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
正在努力成为一个优秀的废物
博客园    首页    新随笔    联系   管理    订阅  订阅
三个线程循环打印ABC问题

问题描述:三个线程,第一个线程只能打印A,第二个线程只能打印B,第三个线程只能打印C,如何循环打印出一定次数的ABC?

例如:打印循环3次,输出为ABCABCABC

lock方式:

public class PrintABC {
    private Integer times;

    private Integer flag;

    private Lock lock;

    public PrintABC(Integer times, ReentrantLock lock) {
        this.flag = 0;
        this.times = times;
        this.lock = lock;
    }

    public void printA() {
        print("A", 0);
    }

    public void printB() {
        print("B", 1);
    }

    public void printC() {
        print("C", 2);
    }

    public void print(String s, int value) {
        for (int i = 0; i < times;) {
            lock.lock();
            if (flag == value) {
                System.out.printf("%s", s);
                flag = (flag + 1) % 3;
                i++;
            }
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        PrintABC printABC = new PrintABC(new Integer(10), new ReentrantLock());
        new Thread(printABC::printA).start();
        new Thread(printABC::printB).start();
        new Thread(printABC::printC).start();
    }
}

lock + condition方式:

public class PrintABC2 {
    private Lock lock;

    private int flag;

    private int times;

    Condition[] condition = new Condition[3];

    public PrintABC2(int times, Lock lock) {
        this.lock = lock;
        this.times = times;
        this.flag = 0;
        for (int i = 0; i < 3; i++) {
            this.condition[i] = lock.newCondition();
        }
    }

    public void printA() {
        print("A", 0);
    }

    public void printB() {
        print("B", 1);
    }

    public void printC() {
        print("C", 2);
    }

    public void print(String s, int value) {
        try {
            for (int i = 0; i < times; i++) {
                lock.lock();
                if (flag != value) {
                    condition[value].await();
                }
                System.out.printf("%s", s);
                flag = (flag + 1) % 3;
                condition[flag].signal();
                lock.unlock();
            }
        } catch (InterruptedException e) {
            System.out.println(e);
        }
    }
    public static void main(String[] args) {
        PrintABC2 printABC = new PrintABC2(new Integer(10), new ReentrantLock());
        new Thread(printABC::printA).start();
        new Thread(printABC::printB).start();
        new Thread(printABC::printC).start();
    }
}

信号量方式:

public class PrintABC3 {
    private int times;

    private Semaphore[] semaphores = new Semaphore[] {new Semaphore(1), new Semaphore(0), new Semaphore(0)};

    public PrintABC3(int times) {
        this.times = times;
    }

    public void printA() {
        print("A", 0);
    }

    public void printB() {
        print("B", 1);
    }

    public void printC() {
        print("C", 2);
    }

    public void print(String s, int currendId) {
        int nextId = (currendId + 1) % 3;
        try {
            for (int i = 0; i < times; i++) {
                semaphores[currendId].acquire();
                System.out.printf("%s", s);
                semaphores[nextId].release();
            }
        } catch (InterruptedException e) {
            System.out.println(e);
        }
        return;
    }

    public static void main(String[] args) {
        PrintABC3 printABC = new PrintABC3(10);
        new Thread(printABC::printA).start();
        new Thread(printABC::printB).start();
        new Thread(printABC::printC).start();
    }
}

 

posted on 2020-04-16 20:13  你算哪根小毛线  阅读(612)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3