日期:2014-05-20  浏览次数:21107 次

使用ExecutorService,如何获取invokeAll所有线程的id?
Java code

public static int getFinalResultPar(String s) throws FileNotFoundException,InterruptedException,ExecutionException{
        
        ExecutorService executor = Executors.newFixedThreadPool(8);
        Scanner scanner2 = new Scanner(new File(s));
        int fR = 0;
        
        List<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>();
            
        while(scanner2.hasNextLine()){
            final Scanner lineScanner2 = new Scanner(scanner2.nextLine());            
            Callable<Integer> c = new Callable<Integer>() {
                public Integer call() {
                    
                    return getDivisorsSum(calSummedUpSum(calOneLine(lineScanner2)));
                }
            };
            tasks.add(c);
        }
        List<Future<Integer>> futures = executor.invokeAll(tasks);
        
        for (Future<Integer> fs: futures) {
            int res = fs.get();
            fR += res;
        }
        executor.shutdown();
        return fR;
    }



这是小弟的一部分源代码,至于干什么就不用管了,问题是,有没有办法在executor.invokeAll(tasks)之后获得执行tasks的所有线程id?Thread.currentThread.getId吗?谢谢了

------解决方案--------------------
在Callable c的call方法里加一句threadsID.add(Thread.currentThread.getId())
threadsID是ArrayList<Long>