亲宝软件园·资讯

展开

Java并发工具类Future

码拉松 人气:0

前言

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<String> future
                = executor.submit(new Callable<String>() {
            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方法比较特殊:<T> Future<T> submit(Runnable task, T result);这个方法可以先把result这个参数传给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);
		
		// 提交任务后,在任务中操作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

FutureTask是一个实现了Runnable和Future接口的工具类,结合了这两个接口的能力,FutureTask即可以提交给线程任务执行,又可以获取到执行结果。

具体代码示例:

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";
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论