newFixedThreadPool的内部工作 [英] Internal Working of newFixedThreadPool

查看:98
本文介绍了newFixedThreadPool的内部工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我理解newFixedThreadPool(或缓存)的内部流程

Kindly help me in understanding the internal flow of newFixedThreadPool (or Cached)

当我们写下面的语句时,
ExecutorService e = Executors.newFixedThreadPool(3 );

When we write below statements, ExecutorService e=Executors.newFixedThreadPool(3);


  1. e.execute(runaable1);

  2. e.execute(runaable2);

  3. e.execute(runaable3);

  4. e.execute(runaable4);

  5. e.execute( runaable5);

  1. e.execute(runaable1);
  2. e.execute(runaable2);
  3. e.execute(runaable3);
  4. e.execute(runaable4);
  5. e.execute(runaable5);

直到3个执行方法,将创建三个线程,当第四个执行方法被调用时,没有新线程将被已创建,但工作将等待一个线程是免费的。

till 3 execute methods ,three threads will be created,when 4th execute method will be called,no new thread will be created but work will be waiting for a thread to be free.

我不明白这一点没有新的线程会被创建,但工作将等待线程免费。我认为当runnable1将被赋予第一个创建的线程时,一旦runnable1的run方法完成,Thread1的运行也将完成,thread1将无法调用runnable4的run方法。
所以,java如何设法用3个线程执行5个Runnable。

I do not understand this point "no new thread will be created but work will be waiting for a thread to be free." what i think when runnable1 will be given to first created thread,once runnable1's run method will be finished,Thread1's run will also be finished,thread1 will not be able to call run method of runnable4. So, how java manages to execute 5 Runnable with just 3 threads.

推荐答案

FYI:这是一个非常简单的线程池的实现。

FYI: Here is a very simple implementation of a thread pool.

class MyThreadPool implements java.util.concurrent.Executor 
{
    private final java.util.concurrent.BlockingQueue<Runnable> queue;

    public MyThreadPool(int numThreads) {
        queue = new java.util.concurrent.LinkedBlockingQueue<>();
        for (int i=0 ; i<numThreads ; i++) {
            new Thread(new Runnable(){
                @Override
                public void run() {
                    while(true) {
                        queue.take().run();
                    }
                }
            }).start();
        }
    }

    @Override
    public void execute(Runnable command) {
        queue.put(command);
    }
}

这不会编译,因为我没有处理使用InterruptedException,在真正的线程池中,您还希望处理给定命令可能抛出的异常,但它应该让您大致了解线程池的作用。

This won't compile because I didn't deal with InterruptedException, and in a real thread pool, you would also want to deal with exceptions that might be thrown by the given command, but it should give you a rough idea of what a thread pool does.

它创建一个队列和任意数量的工作线程。工作线程彼此竞争以从队列中消耗命令。队列是一个 BlockingQueue ,所以当队列为空时,工作人员都会休眠。

It creates a queue and an arbitrary number of worker threads. The worker threads compete with one another to consume commands from the queue. The queue is a BlockingQueue, so the workers all sleep whenever the queue is empty.

其他线程可能会产生新的命令(即 Runnable 对象),并调用执行(命令)方法将新命令放入队列。这些命令将由工作线程以与它们排队的顺序相同的顺序执行(即,它们的运行方法将被调用)。

Other threads may produce new commands (i.e., Runnable objects), and call the execute(command) method to put the new commands in the queue. The commands will be performed (i.e., their run methods will be called) by the worker threads in approximately* the same order that they were enqueued.


  • 大概是因为工作人员A可以从队列中选择一个新命令,然后在调用命令的 .run()方法。然后,其他工作人员可以从队列中选择其他命令并在调度程序允许工作人员A再次运行之前执行它们。

  • Approximately, because worker A could pick a new command from the queue, and then lose its time slice before calling the command's .run() method. Other workers could then pick other commands from the queue and perform them before the scheduler allows worker A to run again.

这篇关于newFixedThreadPool的内部工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆