简体   繁体   English

ExecutorService Java线程限制

[英]ExecutorService Java thread limit

I am using ExecutorService for creating Thread . 我正在使用ExecutorService创建Thread In the run method, its processing a time consuming operations. run方法中,其处理耗时的操作。 It takes nearly upto 10 seconds to complete it. 完成此过程大约需要10秒钟。 For testing, here I am using Thread.sleep(10000); 为了进行测试,我在这里使用Thread.sleep(10000);

My question is, If I use newFixedThreadPool as 2000, will it really execute 2000 threads at a time? 我的问题是,如果我将newFixedThreadPool用作2000,它将真的一次执行2000个线程吗?

public class ThreadPoolTest {

    public static void main(String[] args) {
        System.out.println(new Date());
        ExecutorService executor = Executors.newFixedThreadPool(2000);
        IntStream.range(0, 2000).forEach(
                i -> {
                    Runnable worker = new WorkerThread("WorkerThread-" + i);
                    executor.submit(worker);//calling execute method of ExecutorService
                }
        );
        executor.shutdown();
        while (!executor.isTerminated()) {   }

        System.out.println("Finished all threads");
        System.out.println("Main thread finished");
        System.out.println(new Date());


    }
}


public class WorkerThread implements Runnable{

    private String name;
    public WorkerThread(String s){
        this.name=s;
    }

    public void run() {
        System.out.println(Thread.currentThread().getName()+" (Start) message = "+name);
        processData();

    }

    private void processData(){
        try {  Thread.sleep(10000);  } catch (InterruptedException e) { e.printStackTrace(); }
    }

}

I am printing the time in this code. 我在这段代码中打印时间。 Its showing that, it took only 10 seconds to complete the entire process. 它表明,仅用了10秒就完成了整个过程。 That means all 2000 threads executed parallelly? 那意味着所有2000个线程并行执行? I have heard that the number of actual threads running will be based on the number of cores in the system. 我听说运行的实际线程数将基于系统中的cores数。 But how did all 2000 threads run parallely? 但是所有2000个线程如何并行运行?

The number of logical CPUs determines the number of threads running at a given moment. 逻辑CPU的数量确定在给定时刻运行的线程数。

However, a CPU can switch threads every 100 microseconds or about 10,000 times per second giving the illusion more threads are running at once. 但是,CPU可以每100微秒或每秒大约10,000次切换线程,从而使幻觉一次运行了更多线程。 Calling sleep is likely to cause the CPU to context switch until after the timeout. 调用sleep可能会导致CPU在超时之前进行上下文切换。

NOTE: System.out.println holds a lock for the output, so in reality, only one thread at a time is doing real work, and even then the program is probably being slowed down by the speed your screen will update. 注意:System.out.println为输出保留一个锁,因此实际上,一次只有一个线程在执行实际工作,即使这样,程序也可能因屏幕更新的速度而变慢。 Try removing the println (and the sleep ) and it should complete in a fraction of the time. 尝试删除println (和sleep ),它应在短时间内完成。

But how did all 2000 threads run parallely? 但是所有2000个线程如何并行运行?

Almost all of them were asleep. 几乎所有人都睡着了。 The only busy thread was likely to be the one updating the screen with the buffered println messages. 唯一繁忙的线程可能是使用缓冲的println消息更新屏幕的线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM