Java ExecutorService堆空间问题 [英] Java ExecutorService heap space problems

查看:165
本文介绍了Java ExecutorService堆空间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Java的mulithreading问题。我有以下工人类:

  public class ThreadWorker实现Runnable {

//某些代码这里

public void run(){
//调用ThreadWorker本身的一些递归方法,
//最终将停止
{
}

要处理线程,我使用 ExecutorService

  public static int THREAD_NUMBER = 4; 
public static ExecutorServide es = Executors.newFixedThreadPool(THREAD_NUMBER);

添加 ThreadWroker 类的实例发生在这里:

  public void recursiveMethod(Arraylist&Integers>元素,MyClass数据){
if(elements.size()= = 0&& data.qualifies()){
ThreadWorker tw = new ThreadWorker(data);
es.execute(tw);
返回;
}



for(int i = 0; i< elements.size(); i ++){
// some code to prevent my问题
MyClass data1 = new MyClass(data);
MyClass data2 = new MyClass(data);
ArrayList< Integer> newElements =(ArrayList< Integer>)elements.clone();
data1.update(elements.get(i));
data2.update(-1 * elements.get(i));
newElements.remove(i);
recursiveMethod(newElements,data1);
recursiveMethod(newElements,data2);
{
}

问题是递归树的深度是相当大,因为它的宽度,所以很多 ThreadWorkers 添加到 ExecutorService 中,所以在一段时间后大的输入a获取

 线程中的异常pool-1-thread-2java.lang.OutOfMemoryError:Java堆空间

这是因为我认为是因为一个巨大的数量 ThreadWorkers 我添加到 ExecutorSirvice 要执行,所以内存不足。每个 ThreadWorker 需要大约40 Mb的RAM。



有没有办法获得多少线程(实现runnable接口的类的实例)已添加到 ExecutorService ?所以我可以在上面的代码中添加它(int// some code to prevent my problem),as

  while(ExecutorService中的线程数> 10){
Thread.sleep(10000);
}

所以我不会深入或广泛地用我的递归和预防



真诚的,Sergey Aganezov jr。

解决方案

p>如何创建 ThreadPoolExecutor BlockingQueue 支持,使用 ThreadPoolExecutor.CallerRunsPolicy



这样,当没有可用的工作线程可以运行任务on,主线程(正在添加新作业)运行任务本身,这阻止了添加任何更多的作业。



有关构造函数选项的更多详细信息for ThreadPoolExecutor 在其Javadoc页面。


I hava a Java mulithreading question. I have the following worker class:

public class ThreadWorker implements Runnable {

    //some code in here

    public void run(){
      // invokes some recursion method in the ThreadWorker itself,
      // which will stop eventually
    {
}

To work with threads I'm using an ExecutorService:

public static int THREAD_NUMBER = 4;
public static ExecutorServide es = Executors.newFixedThreadPool(THREAD_NUMBER);

Adding instances of ThreadWroker class happens here:

public void recursiveMethod(Arraylist<Integers> elements, MyClass data){
     if (elements.size() == 0 && data.qualifies()){
         ThreadWorker tw = new ThreadWorker(data);
         es.execute(tw);
         return;
     }



     for (int i=0; i< elements.size(); i++){
          // some code to prevent my problem
          MyClass data1 = new MyClass(data);
          MyClass data2 = new MyClass(data); 
          ArrayList<Integer> newElements = (ArrayList<Integer>)elements.clone();
          data1.update(elements.get(i));
          data2.update(-1 * elements.get(i));
          newElements.remove(i);
          recursiveMethod(newElements, data1);
          recursiveMethod(newElements, data2);     
     {    
}

The problem is that the depth of the recursion tree is quite big, so as it's width, so a lot of ThreadWorkers are added to the ExecutorService, so after some time on the big input a get

Exception in thread "pool-1-thread-2" java.lang.OutOfMemoryError: Java heap space

which is caused, as I think because of a ginormous number of ThreadWorkers i'm adding to ExecutorSirvice to be executed, so it runs out of memory. Every ThreadWorker takes about 40 Mb of RAM for all it needs.

Is there a method to get how many threads (instances of classes implementing runnable interface) have been added to ExecutorService? So I can add it in the shown above code (int the " // some code to prevent my problem"), as

while ("number of threads in the ExecutorService" > 10){
    Thread.sleep(10000);
}

so I won't go to deep or to broad with my recursion and prevent those exception-throwing situations.

Sincerely, Sergey Aganezov jr.

解决方案

How about creating a ThreadPoolExecutor backed by a BlockingQueue using ThreadPoolExecutor.CallerRunsPolicy.

This way, when there are no worker threads available to run a task on, the main thread (which is adding the new jobs) runs the task itself, which prevents any more jobs from being added.

There are more details on the constructor options for ThreadPoolExecutor on its Javadoc page.

这篇关于Java ExecutorService堆空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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