zoukankan      html  css  js  c++  java
  • 采用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);
        }
    
    }
  • 相关阅读:
    CUBRID学习笔记 41 sql语法之select
    CUBRID学习笔记 40 使用net修改数据
    CUBRID学习笔记 39 net使用dataset 返回查询的数据
    CUBRID学习笔记 38 net调用java的函数过程
    CUBRID学习笔记 36 在net中添加多行记录
    CUBRID学习笔记 37 ADO.NET Schema Provider
    CUBRID学习笔记 35 net驱动错误码和信息 cubrid教程示例
    程序员应该关注的一些事儿
    如何区分一个程序员是“老手“还是“新手“?
    10个调试和排错的小建议
  • 原文地址:https://www.cnblogs.com/seanvon/p/4072050.html
Copyright © 2011-2022 走看看