首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

使用ExecutorService,怎么获取invokeAll所有线程的id

2012-05-13 
使用ExecutorService,如何获取invokeAll所有线程的id?Java codepublic static int getFinalResultPar(Stri

使用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>

热点排行
Bad Request.