zoukankan      html  css  js  c++  java
  • 仿真跑马的代码,还挺有意思的(P724)

    package thread;
    
    import java.util.Random;
    import java.util.concurrent.BrokenBarrierException;
    import java.util.concurrent.CyclicBarrier;
    
    public class Horse implements Runnable {
        private static int counter = 0;
        private final int id = counter++;
        private int strides = 0;
        private static Random rand = new Random(47);
        private static CyclicBarrier barrier;
    
        public Horse(CyclicBarrier b) {
            barrier = b;
        }
    
        public synchronized int getStrides() {
            return strides;
        }
    
        public void run() {
            try {
                while (!Thread.interrupted()) {
                    synchronized (this) {
                        strides += rand.nextInt(3);
                    }
                    barrier.await();
                }
            } catch (InterruptedException e) {
    
            } catch (BrokenBarrierException e) {
                throw new RuntimeException(e);
            }
        }
    
        public String toString() {
            return "Horse" + id + " ";
        }
        public String tracks(){
            StringBuilder s = new StringBuilder();
            for(int i = 0;i < getStrides();i++) s.append("*");
            s.append(id);
            return s.toString();
        }
    }
    package thread;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.*;
    
    public class HorseRace {
        static final int FINISH_LINE = 20;
        private List<Horse> horses = new ArrayList<Horse>();
        private ExecutorService exec = Executors.newCachedThreadPool();
        private CyclicBarrier barrier;
    
        public HorseRace(int nHorses, final int pause) throws Exception {
            barrier = new CyclicBarrier(nHorses, new Runnable() {
                public void run() {
                    StringBuilder s = new StringBuilder();
                    for (int i = 0; i < FINISH_LINE; i++) s.append("=");
                    System.out.println(s);
                    for (Horse horse : horses) {
                        System.out.println(horse.tracks());
                        try {
                            TimeUnit.MILLISECONDS.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    for (Horse horse : horses)
                        if (horse.getStrides() >= FINISH_LINE) {
                            System.out.println(horse + " won!");
                            exec.shutdownNow();
                            return;
                        }
                    try {
                        TimeUnit.SECONDS.sleep(5);
                    } catch (InterruptedException e) {
                        System.out.println("barrier-action sleep interrupted");
                    }
                }
            });
            for(int i = 0;i < nHorses;i++){
                Horse horse = new Horse(barrier);
                horses.add(horse);
                exec.execute(horse);
            }
        }
        public static void main(String[] args){
            int nHorses = 7;
            int pause = 200;
            if(args.length > 0){
                int n = new Integer(args[0]);
                nHorses = n > 0 ? n: nHorses;
            }
            if(args.length > 1){
                int p = new Integer(args[1]);
                pause = p > -1 ? p : pause;
            }
            try {
                new HorseRace(nHorses,pause);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    洛谷 1339 最短路
    洛谷 1330 封锁阳光大学 图论 二分图染色
    洛谷 1262 间谍网络 Tarjan 图论
    洛谷 1373 dp 小a和uim之大逃离 良心题解
    洛谷 1972 莫队
    洛谷 2158 数论 打表 欧拉函数
    洛谷 1414 数论 分解因数 水题
    蒟蒻的省选复习(不如说是noip普及组复习)————连载中
    关于筛法
    关于整数划分的几类问题
  • 原文地址:https://www.cnblogs.com/shimu/p/10584479.html
Copyright © 2011-2022 走看看