Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 重写执行器的执行方法_Java_Multithreading_Memory_Threadpoolexecutor - Fatal编程技术网

Java 重写执行器的执行方法

Java 重写执行器的执行方法,java,multithreading,memory,threadpoolexecutor,Java,Multithreading,Memory,Threadpoolexecutor,我需要重写executor的execute方法,在这里我需要更改这样的行为,即只有当队列已满时才会创建大于核心池大小的线程 然而,在实时应用程序中,这种行为是不可取的,因为它会导致队列中存在的任务无休止地等待 我已将execute方法更改如下: public void execute(Runnable command) { System.out.println("ActiveCount : " + getActiveCount() + " PoolSize : " + g

我需要重写executor的execute方法,在这里我需要更改这样的行为,即只有当队列已满时才会创建大于核心池大小的线程

然而,在实时应用程序中,这种行为是不可取的,因为它会导致队列中存在的任务无休止地等待

我已将execute方法更改如下:

public void execute(Runnable command)
    {
        System.out.println("ActiveCount : " + getActiveCount() + " PoolSize : " + getPoolSize() 
                    + " QueueSize : " + getQueue().size() +" Idle Threads : " +(getPoolSize()-getActiveCount())); 





int c = ctl.get();
              if (workerCountOf(c) < corePoolSize) {
                  if (addWorker(command, true))
                      return;
                  c = ctl.get();
              }
  else if (isRunning(c) && workQueue.offer(command)) 
  {
      int recheck = ctl.get();

    if (getActiveCount() < workerCountOf(recheck) && isRunning(recheck) && workQueue.offer(command)) {
            return;
        }
    if (addWorker(command, false)) { 
                return;
        }       
    else if (! isRunning(recheck) && remove(command))
        {
              reject(command);
        }
     else if (workerCountOf(recheck) == 0)
        {
              addWorker(null, false);
        }
  }
  else 
  {
      reject(command); // add task to the queue


     }
}
public void execute(Runnable命令)
{
System.out.println(“ActiveCount:+getActiveCount()+”PoolSize:+getPoolSize()
+QueueSize:“+getQueue().size()+”空闲线程:”+(getPoolSize()-getActiveCount());
int c=ctl.get();
if(工作计数(c)
努力实现:
核心线程->非核心线程->队列而不是核心线程->队列->非核心线程。

我不明白为什么需要更改执行方法,我认为,最大池大小不应该优先于队列,正如我在您的代码中看到的那样

我也有同样的问题,您可以点击链接:


我觉得这应该是您最后的选择,首先尝试其他方法。

我不明白您为什么需要更改执行方法,我认为,最大池大小不应该优先于队列,正如我在您的代码中看到的那样

我也有同样的问题,您可以点击链接:


我觉得这应该是您最后的选择,请先尝试其他操作。

这不是默认行为吗..一旦队列已满,则只为线程池创建新线程。那么,听起来好像您错误地使用了线程池。我觉得在他的情况下,需要使用线程来最大化池大小,如果无法创建新线程,则,队列必须用于等待任何线程空闲…我知道这不是池默认行为,但一般来说,这是常见的要求。这不是默认行为吗..一旦队列已满,则只会为线程池创建新线程。听起来你使用的线程池不正确。我觉得在他的情况下,需要使用线程来最大化池大小,如果不能创建新线程,则必须使用队列来等待任何线程空闲…我知道这不是池默认行为,但一般来说,这是常见的要求。