千家信息网

Java多线程中Future设计模式怎么用

发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,这篇文章将为大家详细讲解有关Java多线程中Future设计模式怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。Future -> 代表的是未来的一个凭据pub
千家信息网最后更新 2026年10月31日Java多线程中Future设计模式怎么用

这篇文章将为大家详细讲解有关Java多线程中Future设计模式怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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

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

AsynFuture -> Future具体实现类

public class AsynFuture implements Future {    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  Future submit(final FutureTask task) {        AsynFuture asynFuture = new AsynFuture<>();        new Thread(() -> {            T result = task.call();            asynFuture.done(result);        }).start();        return asynFuture;    }    /**     * 运行完 自动回调     * 无需进程等待     */    public  Future submit(final FutureTask task, final Consumer consumer) {        AsynFuture asynFuture = new AsynFuture<>();        new Thread(() -> {            T result = task.call();            asynFuture.done(result);            consumer.accept(result);        }).start();        return asynFuture;    }}

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

public interface FutureTask {    T call();}

需要时回调:

/** * Future        -> 代表的是未来的一个凭据 * FutureTask    -> 将你的调用逻辑进行了隔离 * FutureService -> 桥接Future和FutureTask */public class SyncInvoker {    public static void main(String[] args) throws InterruptedException {        FutureService futureService = new FutureService();        Future 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多线程中Future设计模式怎么用"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

代表 凭据 篇文章 逻辑 运行 隔离 模式 线程 设计模式 设计 更多 进程 不错 实用 内容 文章 知识 参考 帮助 有关 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 哈尔滨大商圈互联网科技有限公司 互联网的科技创新 中国 科技 互联网 九亚互联网科技 互联网科技公司做什么 金融科技和互联网金融 互联网科技生活的内容 宁波青枫互联网科技有限公司 字节跳动和互联网科技 花神(南京)互联网科技有限公司 亳州好赞互联网科技有限公司 内蒙古九一互联网科技 玉溪新华互联网科技好不好 保险科技服务互联网营销体系 新氧科技和美国互联网有关系吗 珠海东科互联网科技有限公司 银川新华互联网科技学校是国营吗 三亚科技创新互联网基地 科技互联网专业工作会议发言稿 株洲百竞科技的互联网营销顾问 西安纸贵互联网科技有限公司 中关村互联网 科技集团 北京互联网科技公司 以房互联网科技 上海亿帐通互联网科技 互联网科技教育视频 卡尔迅互联网科技有限公司 图游互联网科技 信承诺互联网科技有限公司 互联网科技品牌
0