0

这里的时候拒绝是我的代码:得到错误FutureTask @ 2c7b84de使用线程池

class Processor implements Runnable { 

    private int id; 
    private Integer interaction; 
    private Set<Integer> subset; 
    private static volatile AtomicBoolean notRemoved = new AtomicBoolean(true); 

    public Object<E> dcp; 
    public Iterator<Integer> iterator; 



    public Processor(int id, Integer interaction, Set<Integer> subset, Object<E> dcp, Iterator<Integer> iterator) { 
     this.id = id; 
     this.interaction = interaction; 
     this.subset= subset; 
     this.dcp = dcp; 
     this.iterator = iterator; 
    } 

    public void run() { 
     while (Processor.notRemoved.get()){ 
      System.out.println("Starting: " + this.id); 
      if (this.dcp.PA.contains(this.interaction)){ 
       this.subset.add(this.interaction); 
       this.dcp.increaseScore(this.subset); 
       if (!this.subset.contains(this.interaction) && Processor.notRemoved.get()){ 
        Processor.notRemoved.set(false); 
        iterator.remove(); 
       } 
      } 

      System.out.println("Completed: " + this.id); 
     } 
    } 
} 


public class ConcurrentApp { 

    public void mainFunction (Object<E> dcp, int threads) { 

     ExecutorService executor = Executors.newFixedThreadPool(threads); 

     int i =1; 
     while ((dcp.PA.size() > i) && (i <= dcp.R)){ 
      for (Iterator<Integer> iterator = dcp.PA.iterator(); iterator.hasNext();){ 
       Integer interaction = iterator.next(); 
       ArrayList<Integer> removed = new ArrayList<Integer>(dcp.PA); 
       removed.remove(interaction); 
       ArrayList<Set<Integer>> subsets = dcp.getSubsets(removed, i); 
       for (int j = 0; j< subsets.size(); j++){ 
        executor.submit(new Processor(j, interaction, subsets.get(j), dcp, iterator)); 
       } 
       executor.shutdown(); 
       System.out.println("All tasks submitted"); 
       try { 
        executor.awaitTermination(1, TimeUnit.DAYS); 
       } catch (InterruptedException e) { 
        System.out.println("HERE"); 
        e.printStackTrace(); 
       } 
      } 
      System.out.println("All tasks completed"); 
      i++; 
     } 
    } 
} 

当我在ConcurrentApp运行mainFunction,我得到以下错误: 异常线程“main”的java.util.concurrent .RejectedExecutionException:任务[email protected]拒绝自[email protected] [已终止,池大小= 0,活动线程数= 0,排队任务数= 0,已完成任务数= 8]

我知道这是因为我没有使用执行tor.shutdown()正确,但我不知道为什么?

编辑:我打印时,每个线程启动并完成其任务。这里是控制台输出:

Starting: 1 
Starting: 2 
All tasks submitted 
Starting: 0 
Completed: 2 
Completed: 1 
Completed: 0 
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task [email protected] rejected from [email protected][Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 8] 

这至少表明线程池中的3个线程在错误消失之前完成了他们的任务。

+0

“volatile AtomicBoolean”不是必需的。使用其中一个或另一个 – efekctive

+0

@efekctive OH YEAH!感谢收获。我忘了AtomicBoolean已经是volatile了。 –

+0

你会尝试以下操作:切换提交执行? – efekctive

回答

0

我想出了问题在这里!这只是因为我在所有任务完成之前在while循环中调用了executor.shutdown()。所以新代码是:

public void multiRemoveParents (DirectCausalPredictor<BayesianScoresNew> dcp, int threads) { 

     ExecutorService executor = Executors.newFixedThreadPool(threads); 

     int i =1; 
     while ((dcp.PA.size() > i) && (i <= dcp.R)){ 
      for (Iterator<Integer> iterator = dcp.PA.iterator(); iterator.hasNext();){ 
       Integer interaction = iterator.next(); 
       ArrayList<Integer> removed = new ArrayList<Integer>(dcp.PA); 
       removed.remove(interaction); 
       ArrayList<Set<Integer>> subsets = dcp.getSubsets(removed, i); 
       for (int j = 0; j< subsets.size(); j++){ 
        try { 
         executor.submit(new Processor(j, interaction, subsets.get(j), dcp, iterator)); 
        } catch (RejectedExecutionException e){ 
         System.out.println("Task was rejected"); 
        } 
       } 
      } 
      System.out.println("All tasks completed"); 
      i++; 
     } 
     executor.shutdown(); 
     System.out.println("All tasks submitted"); 
     try { 
      executor.awaitTermination(1, TimeUnit.DAYS); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    }