zoukankan      html  css  js  c++  java
  • 线程间通讯(交替打印ABC)

    package com.kaibing.thread;
    
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class ABCAlternate {
    
        public static void main(String[] args) {
            Alternate alternate = new Alternate();
    
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i=0;i<20;i++){
                        alternate.printA(i);
                    }
                }
            },"A").start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i=0;i<20;i++){
                        alternate.printB(i);
                    }
                }
            },"B").start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i=0;i<20;i++){
                        alternate.printC(i);
                    }
                }
            },"C").start();
        }
    
    }
    class Alternate{
    
        private int number = 1;
    
        private Lock lock = new ReentrantLock();//
    
        private Condition condition1 = lock.newCondition();
        private Condition condition2 = lock.newCondition();
        private Condition condition3 = lock.newCondition();
    
    
        public void printA(int totalLoop){
    
            lock.lock();//加锁
    
            try{
                if (number != 1){
                    condition1.await();
                }
                System.out.println(Thread.currentThread().getName() + " == 	A	" + "第" + totalLoop + "次循环");
    
                number = 2;
                condition2.signal();//唤醒2
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();//释放锁
            }
    
        }
    
        public void printB(int totalLoop){
    
            lock.lock();//加锁
    
            try{
                if (number != 2){
                    condition2.await();
                }
                System.out.println(Thread.currentThread().getName() + " == 	B	" + "第" + totalLoop + "次循环");
    
                number = 3;
                condition3.signal();//唤醒3
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();//释放锁
            }
    
        }
        public void printC(int totalLoop){
    
            lock.lock();//加锁
    
            try{
                if (number != 3){
                    condition3.await();
                }
                System.out.println(Thread.currentThread().getName() + " == 	C	" + "第" + totalLoop + "次循环");
    
                number = 1;
                condition1.signal();//唤醒1
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();//释放锁
            }
    
        }
    
    
    }
  • 相关阅读:
    EMIF接口的寻址问题
    C6000系类的内联函数
    C6000代码层面优化(一)
    RHCE学习笔记
    找出并关闭Ruby On Rails进程.
    Rails4添加bootstrap支持
    如何删除gnome3面板上的图标?
    出现 ruby cannot load such file -- zlib 问题的解决方法
    Debian安装软件时提示插入光盘
    Debian7桌面屏蔽图标和右键菜单的解决方法。
  • 原文地址:https://www.cnblogs.com/kaibing/p/9258336.html
Copyright © 2011-2022 走看看