1个线程需要2秒,10个线程需要16-18秒。为什么?

松吉特

我的每个线程睡眠2000毫秒,而我有10个这样的线程,因此我希望总睡眠时间至少为20秒,但仅在16-18秒之间。对不起,我要问的是已经被问到的东西。这是我到目前为止的内容:

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

public class MyThreadPoolApp {
    public static void main(String[] args) {
        long execTime = System.currentTimeMillis();
        CountDownLatch latch = new CountDownLatch(1);
        ExecutorService executor = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 10; i++) {
            executor.submit(new Task());
        }
        System.out.println("threads submitted and waiting execution");

        executor.shutdown();        
        try {
            executor.awaitTermination(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
        }
        execTime = System.currentTimeMillis() - execTime;
        System.out.format("%d threads finished execution \n",Task.getCount());
        System.out.println("thread time : " + Task.getTime());
        System.out.println("main time : " + execTime);

    }
}

任务在哪里:

public class Task implements Runnable {

    private static long totalTime; 
    private static int count; 
    public static long getTime(){ return totalTime; }
    public static int getCount(){ return count; }

    public void run() {
        count++;
        long startTime = System.currentTimeMillis();
        try {
            Thread.sleep(2000);
            totalTime += System.currentTimeMillis() - startTime;
        } catch (InterruptedException e) {}
    }
}

我的输出:

threads submitted and waiting execution  
10 threads finished execution   
thread time : 18001  
main time : 2020  
nullptr

因为您搞砸了各种线程对totalTime的并发更新。

尝试这个:

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

public class MyThreadPoolApp {
    public static void main(String[] args) {
        long execTime = System.currentTimeMillis();
        CountDownLatch latch = new CountDownLatch(1);
        ExecutorService executor = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 10; i++) {
            executor.submit(new Task());
        }
        System.out.println("threads submitted and waiting execution");

        executor.shutdown();
        try {
            executor.awaitTermination(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
        }
        execTime = System.currentTimeMillis() - execTime;
        System.out.format("%d threads finished execution \n",Task.getCount());
        System.out.println("thread time : " + Task.getTime());
        System.out.println("main time : " + execTime);
    }
}

class Task implements Runnable {

    private static long totalTime;
    private static int count;
    public static long getTime(){
        synchronized(Task.class){
            return totalTime;
        }
    }
    private static void addTime(long time){
        synchronized(Task.class){
            totalTime = totalTime + time;
        }
    }
    public static int getCount(){ return count; }

    public void run() {
        count++;
        long startTime = System.currentTimeMillis();
        try {
            Thread.sleep(2000);
            addTime(System.currentTimeMillis() - startTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

编辑如您所愿,在run()方法内部进行同步:

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

public class MyThreadPoolApp {
    public static void main(String[] args) {
        long execTime = System.currentTimeMillis();
        CountDownLatch latch = new CountDownLatch(1);
        ExecutorService executor = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 10; i++) {
            executor.submit(new Task());
        }
        System.out.println("threads submitted and waiting execution");

        executor.shutdown();
        try {
            executor.awaitTermination(1, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
        }
        execTime = System.currentTimeMillis() - execTime;
        System.out.format("%d threads finished execution \n",Task.getCount());
        System.out.println("thread time : " + Task.getTime());
        System.out.println("main time : " + execTime);
    }
}

class Task implements Runnable {

    private static long totalTime;
    private static int count;
    public static long getTime(){
        synchronized(Task.class){
            return totalTime;
        }
    }
    public static int getCount(){ return count; }

    public void run() {
        count++;
        long startTime = System.currentTimeMillis();
        try {
            Thread.sleep(2000);
            synchronized(Task.class){
                totalTime += System.currentTimeMillis() - startTime;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

ionic 2 Blank App 启动需要 10 秒,为什么?

来自分类Dev

为什么需要线程

来自分类Dev

为什么Travis-CI用18秒来构建简单的C代码?

来自分类Dev

理解问题,为什么这两个程序分别需要3和6秒

来自分类Dev

为什么8个线程比2个线程慢?

来自分类Dev

为什么打开/ proc / cpuinfo需要17毫秒?

来自分类Dev

JAVAFX:为什么等待游标需要一个新线程?

来自分类Dev

为什么在Windows上尝试Socket.connect失败需要1秒钟?

来自分类Dev

为什么需要UI线程检查

来自分类Dev

Ruby DateTime:为什么将60秒视为59秒?

来自分类Dev

编程Erlang(2nd,Armstrong)第15-18页:为什么第二秒钟接收到外壳后挂起?

来自分类Dev

为什么Java tzupdater增加leap秒?

来自分类Dev

为什么std :: chrono :: duration基于秒

来自分类Dev

为什么App以40秒的延迟启动?

来自分类Dev

rEFInd加载需要10秒

来自分类Dev

为什么程序在仅加入 1 个线程但有 5 个线程时正常工作

来自分类Dev

Mongodb find() 需要 1 秒

来自分类Dev

Outlook需要识别什么线程?

来自分类Dev

PHP为什么说微秒是1/10000秒?

来自分类Dev

为什么我的程序不会延迟1秒?

来自分类Dev

为什么我的程序不会延迟1秒?

来自分类Dev

为什么设置USER环境变量需要12秒钟?

来自分类Dev

为什么std :: chrono说我的函数需要零纳秒的时间执行?

来自分类Dev

为什么在python中找到epsilon的迭代算法实际上需要大约0秒?

来自分类Dev

简单的mysql查询需要16+秒

来自分类Dev

为什么我的tempmute命令在告诉我10秒钟时将人们静音1秒钟?

来自分类Dev

为什么我的tempmute命令在告诉我10秒钟时将人们静音1秒钟?

来自分类Dev

组件数据需要18秒才能显示在页面上

来自分类Dev

为什么kubernetes需要有2个coredns pod

Related 相关文章

  1. 1

    ionic 2 Blank App 启动需要 10 秒,为什么?

  2. 2

    为什么需要线程

  3. 3

    为什么Travis-CI用18秒来构建简单的C代码?

  4. 4

    理解问题,为什么这两个程序分别需要3和6秒

  5. 5

    为什么8个线程比2个线程慢?

  6. 6

    为什么打开/ proc / cpuinfo需要17毫秒?

  7. 7

    JAVAFX:为什么等待游标需要一个新线程?

  8. 8

    为什么在Windows上尝试Socket.connect失败需要1秒钟?

  9. 9

    为什么需要UI线程检查

  10. 10

    Ruby DateTime:为什么将60秒视为59秒?

  11. 11

    编程Erlang(2nd,Armstrong)第15-18页:为什么第二秒钟接收到外壳后挂起?

  12. 12

    为什么Java tzupdater增加leap秒?

  13. 13

    为什么std :: chrono :: duration基于秒

  14. 14

    为什么App以40秒的延迟启动?

  15. 15

    rEFInd加载需要10秒

  16. 16

    为什么程序在仅加入 1 个线程但有 5 个线程时正常工作

  17. 17

    Mongodb find() 需要 1 秒

  18. 18

    Outlook需要识别什么线程?

  19. 19

    PHP为什么说微秒是1/10000秒?

  20. 20

    为什么我的程序不会延迟1秒?

  21. 21

    为什么我的程序不会延迟1秒?

  22. 22

    为什么设置USER环境变量需要12秒钟?

  23. 23

    为什么std :: chrono说我的函数需要零纳秒的时间执行?

  24. 24

    为什么在python中找到epsilon的迭代算法实际上需要大约0秒?

  25. 25

    简单的mysql查询需要16+秒

  26. 26

    为什么我的tempmute命令在告诉我10秒钟时将人们静音1秒钟?

  27. 27

    为什么我的tempmute命令在告诉我10秒钟时将人们静音1秒钟?

  28. 28

    组件数据需要18秒才能显示在页面上

  29. 29

    为什么kubernetes需要有2个coredns pod

热门标签

归档