JAVA8-多线程Future设计模式原理,自定义实现一个Future程序

admin
2022-11-20 / 0 评论 / 87 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年11月20日,已超过521天没有更新,若内容或图片失效,请留言反馈。

JAVA8-多线程Future设计模式原理,自定义实现一个Future程序。

future

自定义实现Future,理解设计模式原理。

Future模式实现

自定义代码示例:

package com.example.study.java8.funture;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 自定义模拟future,理解Future的使用。
 */
public class FutureInAction {
    public static void main(String[] args) throws InterruptedException {
        Funture<String> funture = invoke(() -> {
            try {
                //模拟操作计算很长时间
                Thread.sleep(10000);
                return "I'm finished";
            } catch (Exception e) {
                e.printStackTrace();
                return "Error";
            }
        });
        //操作计算很长时间,还没操作计算完成,只返回了future,虽然此时返回值为null,但是可以接着执行官其它操作,不会阻塞后续操作。
        System.out.println(funture.get());
        System.out.println(funture.get());
        System.out.println(funture.get());

        //知道操作计算完成后,将值返回
        while (!funture.isDone()) {
            Thread.sleep(10);
        }
        System.out.println(funture.get());
    }

    private static <T> Funture<T> invoke(Callable<T> callable) {
        AtomicReference<T> result = new AtomicReference<>();
        AtomicBoolean finished = new AtomicBoolean(false);

        Thread t = new Thread(() -> {
            T value = callable.action();
            result.set(value);
            finished.set(true);
        });
        t.start();

        Funture funture = new Funture() {
            @Override
            public Object get() {
                return result.get();
            }

            @Override
            public boolean isDone() {
                return finished.get();
            }
        };
        return funture;
    }

    private interface Funture<T> {
        T get();

        boolean isDone();
    }

    private interface Callable<T> {
        T action();
    }
}

输出结果:

null  //这里没有阻塞
null  //这里没有阻塞
null  //这里没有阻塞
I'm finished  //这里没有阻塞

null之前没有阻塞,可以之前后续的其它操作。

用没有使用Future的方式实现

package com.example.study.java8.funture;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 自定义模拟future,理解Future的使用。
 */
public class FutureInAction {
    public static void main(String[] args) throws InterruptedException {
        //以前没有future,阻塞方式测试
        String str = block(() -> {
            try {
                //模拟操作计算很长时间
                Thread.sleep(10000);
                return "I'm finished";
            } catch (Exception e) {
                e.printStackTrace();
                return "Error";
            }
        });
        //这里就会阻塞等返回结果,再执行下面的其他操作。
        System.out.println(str);
    }

    //最早的没有future,实现方式
    private static <T> T block(Callable<T> callable) {
        return callable.action();
    }


    private interface Funture<T> {
        T get();

        boolean isDone();
    }

    private interface Callable<T> {
        T action();
    }
}

输出结果:

I'm finished  //这里会阻塞,等很久操作计算完成,才打印结果,后续的其它操作会被阻塞在这里

这里会阻塞,等很久操作计算完成,才打印结果,后续的其它操作会被阻塞在这里。

1

评论 (0)

取消