zoukankan      html  css  js  c++  java
  • 证明创建runnable实例和普通类时间一样长, 其实吧


    import java.util.concurrent.ConcurrentLinkedQueue;
    import java.util.concurrent.CountDownLatch;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;

    //证明创建runnable实例和普通类时间一样长, 其实这是自己对线程和任务混淆了

    public class Test002 {

    private ConcurrentLinkedQueue<String> queue = new ConcurrentLinkedQueue<String>();
    // private ArrayList<String> queue = new ArrayList<String>();
    // private CyclicBarrier barrier = new CyclicBarrier(10000000);
    private CountDownLatch latch = new CountDownLatch(100000);
    ExecutorService es = Executors.newFixedThreadPool(4);

    public static void main(String[] args) {
    Test002 test001 = new Test002();
    long timeStart = System.currentTimeMillis();
    test001.begin();
    System.out.println("##线程类实例化10000次时间:"+(System.currentTimeMillis()-timeStart));

    timeStart = System.currentTimeMillis();
    test001.begin2();
    System.out.println("##普通类实例化10000次时间:"+(System.currentTimeMillis()-timeStart));

    }

    public void begin(){
    for (int i = 0; i < 10000; i++) {
    Runnable001 runnable001 = this.new Runnable001(i);
    }
    }
    public void begin2(){
    for (int j = 0; j < 10000; j++) {
    Runnable002 Runnable002 = this.new Runnable002(j);
    // new Thread(); // 时间主要花在创建线程上,使用runnable接口不占用时间
    }
    }

    private class Runnable001 implements Runnable{
    private int value;
    public Runnable001(int value) {
    this.value = value;
    }

    public void run() {

    try {
    // barrier.await();
    } catch (Exception e) {
    e.printStackTrace();
    }
    queue.offer(value + "");
    latch.countDown();//latch计数减一
    }

    }
    private class Runnable002 {
    private int value;
    public Runnable002(int value) {
    this.value = value;
    }

    public void run() {

    try {
    // barrier.await();
    } catch (Exception e) {
    e.printStackTrace();
    }
    queue.offer(value + "");
    latch.countDown();//latch计数减一
    }

    }
    }
  • 相关阅读:
    Win7 vs2017 WDK 1803 1809 驱动开发 出错 KMDF
    http 请求 post get 长度限制
    IO模式和IO多路复用(阻塞IO、非阻塞IO、同步IO、异步IO等概念)
    select/poll 和 epoll 比较
    centos查看端口被哪个应用端口占用命令
    mysql索引知识简单记录
    Spring钩子方法和钩子接口的使用详解
    mysql使用自增Id为什么存储比较快
    分布式Id教程
    如何配置JVM系统属性及获取方式System.getProperty("pname")
  • 原文地址:https://www.cnblogs.com/fengdaren/p/9765922.html
Copyright © 2011-2022 走看看