importjava.util.ArrayList;importjava.util.concurrent.Callable;importjava.util.concurrent.ExecutionException;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;importjava.util.concurrent.Future;publicclassCachedThreadPool{privatestaticintid=0;publicstaticvoidmain(String[]args){newCachedThreadPool().fun();}privatevoidfun(){ExecutorServiceexe=Executors.newCachedThreadPool();ArrayList>list=newArrayList >(); for(inti=0;i<3;i++){list.add(exe.submit(newTaskCall()));}for(Futurefs:list){ try{System.out.println(fs.get());}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(ExecutionExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}exe.shutdown();}privatesynchronizedStringgetId(){return++id+"";}classTaskCallimplementsCallable{ @OverridepublicStringcall()throwsException{//TODOAuto-generatedmethodstubreturngetId();}}}这段代码并没有什么问题,但是为何把getId()函数放到TaskCall内部,却会得到同步失败的输出(223或者333等),这是为什么呢?
白板的微信
一只斗牛犬
相关分类