zoukankan      html  css  js  c++  java
  • java多线程之计算数量

    package Thread.Abort;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.TimeUnit;
    
    //计数类
    class Count {//DATA
        private int count = 0;
        private Random rand = new Random(47);
    
        //
        public synchronized int increment() {
            int temp = count;
            if (rand.nextBoolean())
                Thread.yield();
            return (count = ++temp);
        }
    
        public synchronized int value() {
            return count;
        }
    }
    
    //动作类
    class Entrance implements Runnable {
        private static Count count = new Count();
        private static List<Entrance> entrances = new ArrayList<Entrance>();
        private int number = 0;
        private final int id;
        private static volatile boolean canceled = false;
    
        public static void cancel() {
            canceled = true;
        }
    
        public Entrance(int id) {
            this.id = id;
            entrances.add(this);
        }
    
        @Override
        public void run() {
            while (!canceled) {
                synchronized (this) {
                    ++number;
                }
                System.out.println(this + " Total:" + count.increment());//要显示的数值都要通过同步方法读取才准确
                try {
                    TimeUnit.MICROSECONDS.sleep(100);
                } catch (Exception e) {
                    System.out.println("sleep interrupted");
                }
            }
            System.out.println("Stopping " + this);
        }
    
        public synchronized int getValue() {
            return number;
        }
    
        public String toString() {
            return "Entrance " + id + ": " + getValue();
        }
    
        public static int getTotalCount() {
            return count.value();
        }
    
        public static int sumEntrances() {
            int sum = 0;
            for (Entrance entrance : entrances) {
                sum += entrance.getValue();
            }
            return sum;
        }
    }
    
    public class OrnamentalGarden {
        public static void main(String[] args) throws InterruptedException {
            ExecutorService exec = Executors.newCachedThreadPool();
            for (int i = 0; i < 5; i++) {
                exec.execute(new Entrance(i));
            }
            TimeUnit.SECONDS.sleep(3);
            Entrance.cancel();
            exec.shutdown();
            if (!exec.awaitTermination(250, TimeUnit.MILLISECONDS))
                System.out.println("Some tasks were not terminated!");
            System.out.println("Total:" + Entrance.getTotalCount());
            System.out.println("Sum of Entrances:" + Entrance.sumEntrances());
        }
    }
  • 相关阅读:
    想让进程后台运行,试试Linux的nohup命令,3分钟学会。
    面试官:你能说一下Redis的常见应用场景吗?
    面试被问MySQL 主从复制,怎么破?
    Spring Boot 解决跨域问题的 3 种方案!
    Kafka如果丢了消息,怎么处理的?
    惊呆,这样操作 Nginx 并发数就能达到3w?
    easyexcel 自动设置列宽(转)
    Ubuntu18.04自适应VMware调整桌面大小
    IDEA将maven项目打包时同时带上项目的maven依赖(转)
    python 定时框架APScheduler
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3755946.html
Copyright © 2011-2022 走看看