第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

跪求!newTaskFor實(shí)現(xiàn)線程取消的疑惑感激不盡

跪求!newTaskFor實(shí)現(xiàn)線程取消的疑惑感激不盡

幕布斯6054654 2019-06-12 09:02:02
看書,有個(gè)情況如下:通過newTaskFor方法封裝實(shí)現(xiàn)了線程的取消。給出的demo如下:publicabstractclassSocketUsingTaskimplementsCancellableTask{@GuardedBy("this")privateSocketsocket;protectedsynchronizedvoidsetSocket(Sockets){socket=s;}publicsynchronizedvoidcancel(){try{if(socket!=null)socket.close();}catch(IOExceptionignored){}}publicRunnableFuturenewTask(){returnnewFutureTask(this){publicbooleancancel(booleanmayInterruptIfRunning){try{SocketUsingTask.this.cancel();}finally{returnsuper.cancel(mayInterruptIfRunning);}}};}}interfaceCancellableTaskextendsCallable{voidcancel();RunnableFuturenewTask();}@ThreadSafeclassCancellingExecutorextendsThreadPoolExecutor{publicCancellingExecutor(intcorePoolSize,intmaximumPoolSize,longkeepAliveTime,TimeUnitunit,BlockingQueueworkQueue){super(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue);}publicCancellingExecutor(intcorePoolSize,intmaximumPoolSize,longkeepAliveTime,TimeUnitunit,BlockingQueueworkQueue,ThreadFactorythreadFactory){super(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue,threadFactory);}publicCancellingExecutor(intcorePoolSize,intmaximumPoolSize,longkeepAliveTime,TimeUnitunit,BlockingQueueworkQueue,RejectedExecutionHandlerhandler){super(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue,handler);}publicCancellingExecutor(intcorePoolSize,intmaximumPoolSize,longkeepAliveTime,TimeUnitunit,BlockingQueueworkQueue,ThreadFactorythreadFactory,RejectedExecutionHandlerhandler){super(corePoolSize,maximumPoolSize,keepAliveTime,unit,workQueue,threadFactory,handler);}protectedRunnableFuturenewTaskFor(Callablecallable){if(callableinstanceofCancellableTask)return((CancellableTask)callable).newTask();elsereturnsuper.newTaskFor(callable);}}我覺得這段代碼有點(diǎn)繞,感覺很不直白,我個(gè)人感覺可以換一種方法實(shí)現(xiàn):publicabstractclassSocketUsingTaskextendsFutureTaskimplementsCallable{@GuardedBy("this")privateSocketsocket;protectedsynchronizedvoidsetSocket(Sockets){socket=s;}publicsynchronizedvoidcancel(){try{if(socket!=null)socket.close();}catch(IOExceptionignored){}}@Overridepublicbooleancancel(booleanmayInterruptIfRunning){try{cancel();}finally{returnsuper.cancel(mayInterruptIfRunning);}}}我想知道《Java并發(fā)編程實(shí)戰(zhàn)》中給出的這個(gè)demo的最大的優(yōu)點(diǎn)在哪?為什么要這樣實(shí)現(xiàn)?因?yàn)楦杏X存在一些多余的操作。謝謝
查看完整描述

2 回答

?
小怪獸愛吃肉

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超1個(gè)贊

我覺得demo的寫法有2點(diǎn):第一demo把Callable重新用CancellableTask擴(kuò)展了一下,如果你要增加新的功能就可以做到面向接口編程的效果,你直接實(shí)現(xiàn)了擴(kuò)展性肯定沒這個(gè)好;第二我看newTaskFor那個(gè)就是生成RunnableFuture的工廠,這個(gè)demo中貌似沒有用到
                            
查看完整回答
反對(duì) 回復(fù) 2019-06-12
  • 2 回答
  • 0 關(guān)注
  • 362 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)