newFixedThreadPool.setCorePoolSize()不使用线程,而是创建新主题,这可能会增加开销 [英] newFixedThreadPool.setCorePoolSize() doesn't make use of the threads, creates new theads which may be overhead
问题描述
newFixedThreadPool.setCorePoolSize()不使用线程,而是创建新主题.
newFixedThreadPool.setCorePoolSize() doesn't make use of the threads, creates new theads.
说明: 我为大小2创建了一个newFixedThreadPool,如果该池的两个线程都忙,我可以使用setCorePoolSize()向该池中再添加两个线程.在此过程中,似乎没有重用线程,也可能没有终止某些线程并创建新线程,我将用代码进行解释.
Explanation: I make a newFixedThreadPool for size 2 and if both the threads of this pool are busy I add two more threads to this pool using setCorePoolSize(). In this process it doesn't seem to reuse the threads or may be terminating some threads and creating new which I will explain with code.
public class IncreasePoolSize
{
static ExecutorService service = null;
public static void main(String[] args) throws JMSException, InterruptedException
{
int NoOfth = 2;
int noOfTimesToInc = 0;
System.out.println("Start");
service = Executors.newFixedThreadPool(NoOfth);
for (;;)
{
if ( ((ThreadPoolExecutor)service).getActiveCount() >= NoOfth )
{
if (noOfTimesToInc < 1)
{
System.out.println("Increased Threads-" + (noOfTimesToInc + 1) + " time(s)");
NoOfth += 2;
System.out.println("NoOfTh-" + NoOfth);
((ThreadPoolExecutor)service).setCorePoolSize(NoOfth);
System.out.println("Total no of theads after increasing-" + ((ThreadPoolExecutor)service).getCorePoolSize());
noOfTimesToInc++;
}
}
else if ( ((ThreadPoolExecutor)service).getActiveCount() <= NoOfth)
{
service.execute(new ConcreteThread());
}
}
}
}
class ConcreteThread implements Runnable
{
public void run()
{
try
{
System.out.println("Thread No-" + Thread.currentThread().getId());
Thread.sleep(5000);
System.out.println("Thread No-" + Thread.currentThread().getId() + " finished");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
从输出中可以看到,一旦11号和12号线程开始工作,我将数字加2,因此13号和14号线程开始工作,但是在那之后,我总是创建新线程,而不是使用11号和12号线程,重用线程13和14.
It is seen in the output once thread no 11 and 12 start working, I increase the number by two and so thread 13 and 14 start working, but after that, i always creates new thread instead of using thread 11 and 12 and reuses thread 13 and 14.
Start
Thread No-11
Thread No-12
Increased Threads-1 time(s)
NoOfTh-4
Total no of theads after increasing-4
Thread No-13
Thread No-14
Thread No-11 finished
Thread No-12 finished
Thread No-13 finished
Thread No-14 finished
Thread No-15
Thread No-16
Thread No-13
Thread No-14
Thread No-15 finished
Thread No-16 finished
Thread No-13 finished
Thread No-14 finished
Thread No-17
Thread No-18
Thread No-13
Thread No-14
Thread No-17 finished
Thread No-18 finished
Thread No-13 finished
Thread No-14 finished
Thread No-19
Thread No-20
Thread No-13
Thread No-14
Thread No-19 finished
Thread No-20 finished
Thread No-13 finished
Thread No-14 finished
Thread No-21
Thread No-22
Thread No-13
Thread No-14
Thread No-21 finished
Thread No-22 finished
Thread No-13 finished
Thread No-14 finished
Thread No-23
Thread No-24
Thread No-13
Thread No-14
推荐答案
代码的一个问题是,您设置了核心池大小,但没有设置最大池大小. newFixedThreadPool
使用相同数量的核心和最大池大小,并且您在某种程度上违反了合同.
One issue with your code is that you set the core pool size but not the maximum pool size. A newFixedThreadPool
uses the same number of core and maximum pool size and you somewhat break that contract.
如果添加:
service.setMaximumPoolSize(NoOfth);
设置核心池大小后,将按预期重用相同的4个线程.
after setting the core pool size, the same 4 threads will be reused as you expect.
注意:这实际上是已报告的错误
Note: this is actually a reported bug
如果corePoolSize > maximumPoolSize
,则ThreadPoolExecutor和setMaximumPoolSize
的构造函数将引发IllegalArgumentException,但setCorePoolSize
不会.
The constructor of ThreadPoolExecutor and setMaximumPoolSize
throw an IllegalArgumentException if corePoolSize > maximumPoolSize
, but setCorePoolSize
does not.
这篇关于newFixedThreadPool.setCorePoolSize()不使用线程,而是创建新主题,这可能会增加开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!