Java多线程 Future Java多线程之Future设计模式

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Java多线程 Future Java多线程之Future设计模式

冬日毛毛雨   2021-10-28 我要评论
想了解Java多线程之Future设计模式的相关内容吗,冬日毛毛雨在本文为您仔细讲解Java多线程 Future的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Java多线程,Future设计模式,下面大家一起来学习吧。

Future -> 代表的是未来的一个凭据

public interface Future<T> {
    T get() throws InterruptedException;
}

AsynFuture -> Future具体实现类

public class AsynFuture<T> implements Future<T> {

    private volatile boolean done = false;

    private T result;

    public void done(T result){
        synchronized (this){
            this.result = result;
            this.done = true;
            this.notifyAll();
        }
    }
    /**
     * 轮询 没有完成等待
     */
    @Override
    public T get() throws InterruptedException {
        synchronized (this) {
            while (!done) {
                this.wait();
            }
        }
        return result;
    }
}

FutureService -> 桥接Future和FutureTask

public class FutureService {

    /**
     * 需进程等待
     */
    public <T> Future<T> submit(final FutureTask<T> task) {

        AsynFuture<T> asynFuture = new AsynFuture<>();

        new Thread(() -> {

            T result = task.call();
            asynFuture.done(result);

        }).start();
        return asynFuture;
    }

    /**
     * 运行完 自动回调
     * 无需进程等待
     */
    public <T> Future<T> submit(final FutureTask<T> task, final Consumer<T> consumer) {

        AsynFuture<T> asynFuture = new AsynFuture<>();
        new Thread(() -> {
            T result = task.call();
            asynFuture.done(result);
            consumer.accept(result);
        }).start();
        return asynFuture;
    }
}

FutureTask -> 将你的调用逻辑进行了隔离

public interface FutureTask<T> {

    T call();
}

需要时回调:

/**
 * Future        -> 代表的是未来的一个凭据
 * FutureTask    -> 将你的调用逻辑进行了隔离
 * FutureService -> 桥接Future和FutureTask
 */
public class SyncInvoker {

    public static void main(String[] args) throws InterruptedException {

        FutureService futureService = new FutureService();
        Future<String> future = futureService.submit(() -> {
            try {
                Thread.sleep(10001);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "FINISH";
        });

        System.out.println("==============");
        System.out.println("do other thing.");
        Thread.sleep(1000);

        System.out.println("==============");

        /**
         * 调用也形成了阻塞
         */
        System.out.println(future.get());
    }
}

运行:

==============
do other thing.
==============
FINISH

运行完自动回调:

//**
 * Future        -> 代表的是未来的一个凭据
 * FutureTask    -> 将你的调用逻辑进行了隔离
 * FutureService -> 桥接Future和FutureTask
 */
public class SyncInvoker {

    public static void main(String[] args) throws InterruptedException {

        FutureService futureService = new FutureService();
        futureService.submit(() -> {
            try {
                Thread.sleep(10001);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "FINISH";
        },System.out::println);

        System.out.println("==============");
        System.out.println("do other thing.");
        Thread.sleep(1000);
        System.out.println("==============");
    }
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们