zoukankan      html  css  js  c++  java
  • Phaser铁人三项

    /**
     * 模拟铁人三项
     */
    public class PhaserTest {
    
        private static Random random = new Random(System.currentTimeMillis());
    
        public static void main(String[] args) {
    
            Phaser phaser = new Phaser(5);
            for (int i = 1; i < 5; i++) {
                new Sportsman(i, phaser).start();
            }
            new InjuredSportsman(5, phaser).start();
    
    
        }
    
        static class InjuredSportsman extends Thread {
    
            private int no;
            private Phaser phaser;
    
            InjuredSportsman(int no, Phaser phaser) {
                this.no = no;
                this.phaser = phaser;
            }
    
    
            @Override
            public void run() {
                try {
                    sport(no, phaser, " starts   running ", " ends   running ");
                    sport(no, phaser, " starts   bicycle ", " ends   bicycle");
                    System.out.println("I'm injured ,  i will be exit");
                    phaser.arriveAndDeregister();
    
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
            }
        }
    
        static class Sportsman extends Thread {
    
            private int no;
            private Phaser phaser;
    
            Sportsman(int no, Phaser phaser) {
                this.no = no;
                this.phaser = phaser;
            }
    
    
            @Override
            public void run() {
                try {
                    sport(no, phaser, " starts   running ", " ends   running ");
                    sport(no, phaser, " starts   bicycle ", " ends   bicycle");
                    sport(no, phaser, " starts   long jump  ", " ends   long jump ");
    
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
            }
        }
    
        private static void sport(int no, Phaser phaser, String s, String s2) throws InterruptedException {
            System.out.println(no + s);
            Thread.sleep(random.nextInt(5000));
            System.out.println(no + s2);
            phaser.arriveAndAwaitAdvance();
        }
    }
  • 相关阅读:
    [asp.net core]SignalR一个例子
    [Asp.net core]bootstrap分页
    ${pageContext.request.contextPath}无法解析
    [Java web]Spring+Struts2+Hibernate整合过程(2)
    [Java web]Spring+Struts2+Hibernate整合过程
    java.lang.IllegalStateException: Failed to load ApplicationContext
    [Struts2]配置文件
    unihtmlmemo使用
    ADO序列
    variant和rawbytestring相互转换
  • 原文地址:https://www.cnblogs.com/moris5013/p/11898956.html
Copyright © 2011-2022 走看看