ListenableFuture顾名思义就是可以监听的Future,它是对java原生Future的扩展增强 RateLimiter类似于JDK的信号量Semphore,他用来限制对资源并发访问的线程数,本文介绍RateLimiter使用

Guava并发 ListenableFuture RateLimiter

目录[-]

源码:http://www.jinhusns.com/Products/Download/?type=xcj

概念

ListenableFuture 顾名思义就是可以监听的Future,它是对java原生Future的扩展增强。我们知道Future表示一个异步计算任务,当任务完成时可以得到计算 结果。如果我们希望一旦计算完成就拿到结果展示给用户或者做另外的计算,就必须使用另一个线程不断的查询计算状态。这样做,代码复杂,而且效率低下。使用 ListenableFuture Guava帮我们检测Future是否完成了,如果完成就自动调用回调函数,这样可以减少并发程序的复杂度。

推荐使用第二种方法,因为第二种方法可以直接得到Future的返回值,或者处理错误情况。本质上第二种方法是通过调动第一种方法实现的,做了进一步的封装。

另外ListenableFuture还有其他几种内置实现:

  1. SettableFuture:不需要实现一个方法来计算返回值,而只需要返回一个固定值来做为返回值,可以通过程序设置此Future的返回值或者异常信息

  2. CheckedFuture: 这是一个继承自ListenableFuture接口,他提供了checkedGet()方法,此方法在Future执行发生异常时,可以抛出指定类型的异常。

 RateLimiter类似于JDK的信号量Semphore,他用来限制对资源并发访问的线程数,本文介绍RateLimiter使用

代码示例

?

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
 
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.RateLimiter;
 
public class ListenableFutureDemo {
    public static void main(String[] args) {
        testRateLimiter();
        testListenableFuture();
    }
 
    /**
     * RateLimiter类似于JDK的信号量Semphore,他用来限制对资源并发访问的线程数
     */
    public static void testRateLimiter() {
        ListeningExecutorService executorService = MoreExecutors
                .listeningDecorator(Executors.newCachedThreadPool());
 
        RateLimiter limiter = RateLimiter.create(5.0); // 每秒不超过4个任务被提交
 
        for (int i = 0; i < 10; i++) {
            limiter.acquire(); // 请求RateLimiter, 超过permits会被阻塞
 
            final ListenableFuture<Integer> listenableFuture = executorService
                    .submit(new Task("is "+ i));
        }
    }
 
    public static void testListenableFuture() {
        ListeningExecutorService executorService = MoreExecutors
                .listeningDecorator(Executors.newCachedThreadPool());
 
        final ListenableFuture<Integer> listenableFuture = executorService
                .submit(new Task("testListenableFuture"));
 
         
        //同步获取调用结果
        try {
            System.out.println(listenableFuture.get());
        catch (InterruptedException e1) {
            e1.printStackTrace();
        catch (ExecutionException e1) {
            e1.printStackTrace();
        }
         
        //第一种方式
        listenableFuture.addListener(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("get listenable future's result "
                            + listenableFuture.get());
                catch (InterruptedException e) {
                    e.printStackTrace();
                catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }, executorService);
 
        //第二种方式
        Futures.addCallback(listenableFuture, new FutureCallback<Integer>() {
            @Override
            public void onSuccess(Integer result) {
                System.out
                        .println("get listenable future's result with callback "
                                + result);
            }
 
            @Override
            public void onFailure(Throwable t) {
                t.printStackTrace();
            }
        });
    }
}
 
class Task implements Callable<Integer> {
    String str;
    public Task(String str){
        this.str = str;
    }
    @Override
    public Integer call() throws Exception {
        System.out.println("call execute.." + str);
        TimeUnit.SECONDS.sleep(1);
        return 7;
    }
}

Guava版本

?

1
2
3
4
5
<dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>14.0.1</version>
        </dependency>

Guava并发:ListenableFuture与RateLimiter示例的更多相关文章

  1. 0318 guava并发工具

    并发是一个难题,但是可以通过使用强力简单的抽象来显著的简化,为了简化问题,guava扩展了Future接口,即 ListenableFuture (可以监听的Future).我强烈建议你在你的所有代码 ...

  2. Guava官方文档-RateLimiter类

    转载自并发编程网 – ifeve.com RateLimiter 从概念上来讲,速率限制器会在可配置的速率下分配许可证.如果必要的话,每个acquire() 会阻塞当前线程直到许可证可用后获取该许可证 ...

  3. Guava限流工具RateLimiter使用

    公司最近在推一个限流工具接入,提供的功能有单机限流.集群限流等.想了解一下限流的原理和设计,看了一下wiki里面有提到用了guava的ratelimiter工具,查了一些资料了解了一下 主要的限流算法 ...

  4. 实例:接口并发限流RateLimiter

    需求:接口每秒最多只能相应1个请求 1.创建 全局类对象 import com.google.common.util.concurrent.RateLimiter; import org.spring ...

  5. Swoole 协程的并发调用及使用示例

    示例一: 利用通道pop会自动挂起当前协程,等待生产者推送数据的特性,实现并发调用,并在协程完成后组合结果集. $serv = new Swoole\Http\Server("127.0.0 ...

  6. 《JAVA并发编程实战》示例程序第一、二章

    第一章:简介 程序清单1-1非线程安全的数值序列生成器 import net.jcip.annotations.NotThreadSafe; @NotThreadSafe public class U ...

  7. 《JAVA并发编程实战》示例程序 第三章

    3.1 可见性 程序清单3-1 在没有同步的情况下共享变量(不要这么做) /** * 主线程和读线程都将访问共享变量:ready 和 number * 结果可能 * 1. 主线程先运行完,读线程后运行 ...

  8. 【fork/join】java并发编程-fork/join示例

    package com.chinamobile.epic.tako.common.graphite.query.sync.impl; import com.google.common.collect. ...

  9. RateLimit--使用guava来做接口限流

    转:https://blog.csdn.net/jiesa/article/details/50412027 一.问题描述   某天A君突然发现自己的接口请求量突然涨到之前的10倍,没多久该接口几乎不 ...

随机推荐

  1. java 堆栈分析2

    有了mat.同时我们发现Java有提供jvisualvm, jvisualvm是一个不错的工具: heap dump . thread dump. cpu/mem profile 无所不能. 不过观察 ...

  2. 知方可补不足~sqlserver中触发器的使用

    回到目录 触发器在过去的10年中,即存储过程和ado.net称霸江湖期间是那么的重要,而现在,trigger显得不是那么必要的,我们很少将复杂的业务写在SQL里,当然也会没有机会写到trigger里了 ...

  3. Atitit usrQBK13 html dsl 规范与解决方案

    Atitit usrQBK13 html dsl 规范与解决方案 1.1. Vue  vs anrular1 1.2. 定义html dsl变量1 1.3. 变量赋值1 1.4.  条件渲染指令1 2 ...

  4. Jsp练习——连接数据库模拟登录

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  5. 美团(iPad)顶部界面的简单实现, 及开发时常见bug

    项目功能介绍:1.支持横竖屏旋转,界面正常显示2.通过点击界面顶部"美团",可展示出左右双tableView分别显示服务类列表和子类列表3.通过点击界面顶部"广州&quo ...

  6. 【原创】探索Newlife X组件利器之:XCoder点滴[附下载]

    本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html Newlife XCode组件相关文章目录:http://www.cnblogs ...

  7. 《BI那点儿事》Microsoft 线性回归算法

    Microsoft 线性回归算法是 Microsoft 决策树算法的一种变体,有助于计算依赖变量和独立变量之间的线性关系,然后使用该关系进行预测.该关系采用的表示形式是最能代表数据序列的线的公式.例如 ...

  8. 解决firefox和IE9对icon font字体的跨域访问问题

    何为跨域访问,为什么会有跨域限制?一切还得从浏览器的同源策略说起. 同源策略:是浏览器最核心也是最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能会受到影响,可以说Web是构建在同源策略基础 ...

  9. Unity 3D制作2D游戏的几种方法

    1.使用本身UGUI. 2.把摄像机的投影改为正交投影,不考虑Z轴. 3.使用Untiy自身的2D模式. 4.使用2D TooKit插件.

  10. 轻松自动化---selenium-webdriver(python) (九)

    本节重点: 上传文件 文件上传操作也比较常见功能之一,上传功能没有用到新有方法或函数,关键是思路. 上传过程一般要打开一个本地窗口,从窗口选择本地文件添加.所以,一般会卡在如何操作本地窗口添加上传文件 ...