服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java多线程之Future设计模式

Java多线程之Future设计模式

2022-03-04 18:01冬日毛毛雨 Java教程

这篇文章主要介绍了Java多线程之Future设计模式,Future 代表的是未来的一个凭据,文章主要附上Future具体实现类、桥接Future和FutureTask的代码,需要的朋友可以参考一下

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

?
1
2
3
public interface Future<T> {
    T get() throws InterruptedException;
}

AsynFuture -> Future具体实现类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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 -> 将你的调用逻辑进行了隔离

?
1
2
3
4
public interface FutureTask<T> {
 
    T call();
}

需要时回调:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * 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

运行完自动回调:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//**
 * 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设计模式的文章就介绍到这了,更多相关Java多线程 Future内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/7022215197204938783

延伸 · 阅读

精彩推荐