您好,登錄后才能下訂單哦!
這篇文章主要講解了“Java并發(fā)工具類Future如何使用”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Java并發(fā)工具類Future如何使用”吧!
Future是一個(gè)接口類,定義了5個(gè)方法:
boolean cancel(boolean mayInterruptIfRunning); boolean isCancelled(); boolean isDone(); V get() throws InterruptedException, ExecutionException; V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
通過這5個(gè)方法可以看出,F(xiàn)uture不但可以獲取提交任務(wù)的執(zhí)行結(jié)果,甚至還可以取消任務(wù)。
摘自JDK中Future示例代碼
class ArchiveSearcherImpl implements ArchiveSearcher{ @Override public String search(String target) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return target; } } interface ArchiveSearcher { String search(String target); } class App { public static void main(String[] args) throws InterruptedException { App app = new App(); app.showSearch("search"); } ExecutorService executor = Executors.newSingleThreadExecutor(); ArchiveSearcher searcher = new ArchiveSearcherImpl(); void showSearch(final String target) throws InterruptedException { // 提交任務(wù) Future<String> future = executor.submit(new Callable<String>() { public String call() { return searcher.search(target); } }); displayOtherThings(); // do other things while searching try { // 拿任務(wù)執(zhí)行結(jié)果 displayText(future.get()); // use future } catch (ExecutionException ex) { //cleanup(); return; } } private void displayOtherThings() { System.out.println("do other things"); } private void displayText(String s) { System.out.println(s); } }
注意:future.get()
方法是阻塞式的,如果調(diào)用該方法的時(shí)候任務(wù)尚未執(zhí)行完成,則會(huì)一直等待下去,直到任務(wù)執(zhí)行結(jié)束。
ExecutorService還有一個(gè)sumbit方法比較特殊:<T> Future<T> submit(Runnable task, T result);
這個(gè)方法可以先把result這個(gè)參數(shù)傳給task使用,然后再通過get方法把result返回。
public class FutureTest { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executor = Executors.newSingleThreadExecutor(); MyResult myResult = new MyResult(); MyTask myTask = new MyTask(myResult); // 提交任務(wù)后,在任務(wù)中操作myResult Future<MyResult> future = executor.submit(myTask, myResult); // 最后get方法可以拿到myResult System.out.println(future.get()); } } class MyTask implements Runnable { private MyResult myResult; public MyTask(MyResult myResult) { this.myResult = myResult; } @Override public void run() { myResult.setCode("10000"); } } @Data @ToString class MyResult { private String code; }
FutureTask是一個(gè)實(shí)現(xiàn)了Runnable和Future接口的工具類,結(jié)合了這兩個(gè)接口的能力,F(xiàn)utureTask即可以提交給線程任務(wù)執(zhí)行,又可以獲取到執(zhí)行結(jié)果。
具體代碼示例:
import java.util.concurrent.*; public class FutureTaskTest { public static void main(String[] args) throws ExecutionException, InterruptedException { FutureTask<String> futureTask1 = new FutureTask<>(new MyTask1()); FutureTask<String> futureTask2 = new FutureTask<>(new MyTask2()); ExecutorService executorService = Executors.newFixedThreadPool(2); executorService.submit(futureTask1); executorService.submit(futureTask2); long start = System.currentTimeMillis(); System.out.println(futureTask1.get()); System.out.println(futureTask2.get()); long end = System.currentTimeMillis(); System.out.println(end - start); } } class MyTask2 implements Callable<String> { @Override public String call() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return "MyTask2 over"; } } class MyTask1 implements Callable<String> { @Override public String call() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return "MyTask1 over"; } }
感謝各位的閱讀,以上就是“Java并發(fā)工具類Future如何使用”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)Java并發(fā)工具類Future如何使用這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。