Java多线程之Future设计模式

作者:冬日毛毛雨 时间:2022-07-19 05:28:25 

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

  • AsynFuture -> Future具体实现类

  • FutureService -> 桥接Future和FutureTask

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

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

来源:https://juejin.cn/post/7022215197204938783

标签:Java,多线程,Future
0
投稿

猜你喜欢

  • struts2中使用注解配置Action方法详解

    2023-08-30 00:01:25
  • logback使用filter过滤日志操作

    2022-07-11 03:20:27
  • Java开发工具IntelliJ IDEA安装图解

    2022-06-14 02:30:20
  • Java lombok中@Accessors注解三个属性的作用

    2022-04-20 14:32:27
  • C#控制图像旋转和翻转的方法

    2023-11-26 08:25:08
  • springboot项目配置多个kafka的示例代码

    2023-11-23 23:15:29
  • BeanDefinitionRegistryPostProcessor如何动态注册Bean到Spring

    2023-11-24 12:56:16
  • Java 8 Function函数式接口及函数式接口实例

    2022-04-13 14:55:05
  • Java高性能序列化工具Kryo详情

    2021-11-02 16:42:00
  • Springboot启动后执行方法小结

    2022-09-26 22:12:02
  • Java Condition条件变量提高线程通信效率

    2022-11-26 13:32:46
  • 解决fastjson从1.1.41升级到1.2.28后报错问题详解

    2021-12-30 21:55:35
  • java读取csv文件内容示例代码

    2023-03-13 22:09:14
  • 使用Spring Security OAuth2实现单点登录

    2023-08-13 01:44:34
  • springboot集成mybatis plus和dynamic-datasource注意事项说明

    2023-12-05 03:54:21
  • Java如何使用Query动态拼接SQL详解

    2022-12-31 09:40:14
  • Hadoop源码分析六启动文件namenode原理详解

    2021-08-20 01:03:55
  • 兼容Spring Boot 1.x和2.x配置类参数绑定的工具类SpringBootBindUtil

    2023-11-03 05:35:06
  • springboot集成fastDfs过程代码实例

    2023-02-21 19:42:20
  • Java try catch finally异常处理组合详解

    2021-07-04 18:19:51
  • asp之家 软件编程 m.aspxhome.com