Java设计模式之外观模式示例详解
319
2022-07-26
目录前言Future使用示例FutureTask
前言
Future是一个接口类,定义了5个方法:
boolean cancel(boolean mayInterruptIfRunning);
boolean isCancelled();
boolean isDone();
V get() throws InterruptedException, ExecutionException;
V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
通过这5个方法可以看出,Future不但可以获取提交任务的执行结果,甚至还可以取消任务。
Future使用示例
摘自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 {
// 提交任务
Future = executor.submit(new Callable public String call() { return searcher.search(target); } }); displayOtherThings(); // do other things while searching try { // 拿任务执行结果 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()方法是阻塞式的,如果调用该方法的时候任务尚未执行完成,则会一直等待下去,直到任务执行结束。 ExecutorService还有一个sumbit方法比较特殊: public EspUOxLXeclass FutureTest { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executor = Executors.newSingleThreadExecutor(); MyResult myResult = new MyResult(); MyTask myTask = new MyTask(myResult); // 提交任务后,在任务中操作myResult Future // 最后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 FutureTask是一个实现了Runnable和Future接口的工具类,结合了这两个接口的能力,FutureTask即可以提交给线程任务执行,又可以获取到执行结果。 具体代码示例: import java.util.concurrent.*; public class FutureTaskTest { public static void main(String[] args) throws ExecutionException, InterruptedException { FutureTask FutureTask 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 @Override public String call() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return "MyTask2 over"; } } class MyTask1 implements Callable @Override public String call() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return "MyTask1 over"; } }
= executor.submit(new Callable
public String call() {
return searcher.search(target);
}
});
displayOtherThings(); // do other things while searching
try {
// 拿任务执行结果
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()方法是阻塞式的,如果调用该方法的时候任务尚未执行完成,则会一直等待下去,直到任务执行结束。
ExecutorService还有一个sumbit方法比较特殊:
public EspUOxLXeclass FutureTest {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executor = Executors.newSingleThreadExecutor();
MyResult myResult = new MyResult();
MyTask myTask = new MyTask(myResult);
// 提交任务后,在任务中操作myResult
Future
// 最后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
FutureTask是一个实现了Runnable和Future接口的工具类,结合了这两个接口的能力,FutureTask即可以提交给线程任务执行,又可以获取到执行结果。
具体代码示例:
import java.util.concurrent.*;
public class FutureTaskTest {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask
FutureTask
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
@Override
public String call() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "MyTask2 over";
}
}
class MyTask1 implements Callable
@Override
public String call() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "MyTask1 over";
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~