欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java多线程之Future设计模式

时间:2022-06-12 09:35:17|栏目:JAVA代码|点击:

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("==============");
    }
}

上一篇:Java超详细教你写一个网络购书系统案例

栏    目:JAVA代码

下一篇:Java实例讲解反射机制是怎么一回事

本文标题:Java多线程之Future设计模式

本文地址:http://www.codeinn.net/misctech/204552.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有