• 采用java信号量(semaphore)让线程轮流打印


    semaphore是java.util.concurrent包下的并发工具类。

    A counting semaphore. Conceptually, a semaphore maintains a set of permits. Each acquire() blocks if necessary until a permit is available, and then takes it. Each release() adds a permit, potentially releasing a blocking acquirer. However, no actual permit objects are used; the semaphore just keeps a count of the number available and acts accordingly.
    
    Semaphores are often used to restrict the number of threads than can access some (physical or logical) resource.

    大致意思是semaphore可以持有许可证,可以在初始化时指定,执行一次acquire(),获取一个许可证,线程进入,当许可证为0,则不再允许线程进入,执行一次release释放一个许可证。

    package com.smikevon.concurrent.tools;
    
    import java.util.Random;
    import java.util.concurrent.Semaphore;
    
    /**
     * @description: 有A,B,C三个线程, A线程输出A, B线程输出B, C线程输出C.要求, 同时启动三个线程, 按顺序输出ABC, 循环10次
     * @date       : 2014年9月17日 下午8:23:05
     */
    public class TestSemaphore {
    
        public static void main(String[] args) {
    
            final Semaphore sa = new Semaphore(1);
            final Semaphore sb = new Semaphore(0);
            final Semaphore sc = new Semaphore(0);
    
            final Random random = new Random(System.currentTimeMillis());
    
            new Thread(new Runnable() {
                public void run() {
                    try {
                        while(true){
                            Thread.sleep(random.nextInt(3000));
                            sa.acquire();
                            doing("A");
                            sb.release();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"线程a").start();
    
            new Thread(new Runnable() {
                public void run() {
                    try {
                        while(true){
                            Thread.sleep(random.nextInt(3000));
                            sb.acquire();
                            doing("B");
                            sc.release();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"线程b").start();
    
            new Thread(new Runnable() {
                public void run() {
                    try {
                        while(true){
                            Thread.sleep(random.nextInt(3000));
                            sc.acquire();
                            doing("C");
                            sa.release();
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"线程c").start();
    
        }
    
        /**
         * @Description:打印出线程的名字
         * @param msg
         * @returType:void
         */
        public static void doing(String msg){
            System.out.println(Thread.currentThread().getName()+":"+msg);
        }
    
    }
  • 相关阅读:
    Mysql知识:事务
    Mysql知识 索引补充
    Mysql知识:索引
    Leetcode 78 子集
    每日总结
    个人冲刺-第二阶段(8)
    每日总结
    个人冲刺-第二阶段(7)
    每日总结
    个人冲刺-第二阶段(6)
  • 原文地址:https://www.cnblogs.com/seanvon/p/4072050.html
Copyright © 2020-2023  润新知