• 采用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);
        }
    
    }
  • 相关阅读:
    紫书 例题8-18 UVa 1442 (扫描法)
    紫书 例题8-17 UVa 1609 (构造法)(详细注释)
    紫书 例题8-16 UVa 1608 (递归)
    紫书 例题8-15 UVa 12174 (滑动窗口)
    紫书 例题8-14 UVa 1607 (二分)
    紫书 例题8-13 UVa 11093 (反证法)
    紫书 例题8-12 UVa 12627 (找规律 + 递归)
    Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)
    CodeForces
    CodeForces 444C 线段树
  • 原文地址:https://www.cnblogs.com/seanvon/p/4072050.html
Copyright © 2020-2023  润新知