一:RateLimiter
RateLimiter是guava提供的基于令牌桶算法的实现类,可以非常简单的完成限流特技,并且根据系统的实际情况来调整生成token的速率。
rateLimiter.acquire()该方法会阻塞线程,直到令牌桶中能取到令牌为止才继续向下执行,并返回等待的时间。
二:DelayQueue
是一个无界的BlockingQueue,用于放置实现了Delayed接口的对象,其中的对象只能在其到期时才能从队列中取走。这种队列是有序的,即队头对象的延迟到期时间最长。注意:不能将null元素放置到这种队列中。
Delayed,一种混合风格的接口,用来标记那些应该在给定延迟时间之后执行的对象。此接口的实现必须定义一个 compareTo 方法,该方法提供与此接口的 getDelay 方法一致的排序。
三:几个例子解析
有很多任务,但希望每秒不超过N个
- import com.google.common.util.concurrent.RateLimiter;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- /**
- * Created by wuwf on 17/7/11.
- * 有很多个任务,但希望每秒不超过X个,可用此类
- */
- public class Demo1 {
- public static void main(String[] args) {
- //0.5代表一秒最多多少个
- RateLimiter rateLimiter = RateLimiter.create(0.5);
- List<Runnable> tasks = new ArrayList<Runnable>();
- for (int i = 0; i < 10; i++) {
- tasks.add(new UserRequest(i));
- }
- ExecutorService threadPool = Executors.newCachedThreadPool();
- for (Runnable runnable : tasks) {
- System.out.println("等待时间:" + rateLimiter.acquire());
- threadPool.execute(runnable);
- }
- }
- private static class UserRequest implements Runnable {
- private int id;
- public UserRequest(int id) {
- this.id = id;
- }
- public void run() {
- System.out.println(id);
- }
- }
- }
- import com.google.common.util.concurrent.RateLimiter;
- import com.tianyalei.model.GoodInfo;
- import com.tianyalei.service.GoodInfoService;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import javax.annotation.Resource;
- /**
- * Created by wuwf on 17/7/11.
- */
- @RestController
- public class IndexController {
- @Resource(name = "db")
- private GoodInfoService goodInfoService;
- RateLimiter rateLimiter = RateLimiter.create(10);
- @RequestMapping("/miaosha")
- public Object miaosha(int count, String code) {
- System.out.println("等待时间" + rateLimiter.acquire());
- if (goodInfoService.update(code, count) > 0) {
- return "购买成功";
- }
- return "购买失败";
- }
- @RequestMapping("/add")
- public Object add() {
- for (int i = 0; i < 100; i++) {
- GoodInfo goodInfo = new GoodInfo();
- goodInfo.setCode("iphone" + i);
- goodInfo.setAmount(100);
- goodInfoService.add(goodInfo);
- }
- return "添加成功";
- }
- }
- /**
- * tryAcquire(long timeout, TimeUnit unit)
- * 从RateLimiter 获取许可如果该许可可以在不超过timeout的时间内获取得到的话,
- * 或者如果无法在timeout 过期之前获取得到许可的话,那么立即返回false(无需等待)
- */
- @RequestMapping("/buy")
- public Object miao(int count, String code) {
- //判断能否在1秒内得到令牌,如果不能则立即返回false,不会阻塞程序
- if (!rateLimiter.tryAcquire(1000, TimeUnit.MILLISECONDS)) {
- System.out.println("短期无法获取令牌,真不幸,排队也瞎排");
- return "失败";
- }
- if (goodInfoService.update(code, count) > 0) {
- System.out.println("购买成功");
- return "成功";
- }
- System.out.println("数据不足,失败");
- return "失败";
- }
该场景来自于http://ideasforjava.iteye.com/blog/657384,模拟一个考试的日子,考试时间为120分钟,30分钟后才可交卷,当时间到了,或学生都交完卷了考试结束。
这个场景中几个点需要注意:
- 考试时间为120分钟,30分钟后才可交卷,初始化考生完成试卷时间最小应为30分钟
- 对于能够在120分钟内交卷的考生,如何实现这些考生交卷
- 对于120分钟内没有完成考试的考生,在120分钟考试时间到后需要让他们强制交卷
- 在所有的考生都交完卷后,需要将控制线程关闭
实现思想:用DelayQueue存储考生(Student类),每一个考生都有自己的名字和完成试卷的时间,Teacher线程对DelayQueue进行监控,收取完成试卷小于120分钟的学生的试卷。当考试时间120分钟到时,先关闭Teacher线程,然后强制DelayQueue中还存在的考生交卷。每一个考生交卷都会进行一次countDownLatch.countDown(),当countDownLatch.await()不再阻塞说明所有考生都交完卷了,而后结束考试。
package com.my.base.concurrent.delayQueue; import java.util.Iterator; import java.util.Random; import java.util.concurrent.CountDownLatch; import java.util.concurrent.DelayQueue; import java.util.concurrent.Delayed; import java.util.concurrent.TimeUnit; /** *this project is created for my partactice. *In the project I will write the mybatis by myself * *2014-1-10 下午9:43:48 *@author 孙振超 mychaoyue2011@163.com */ public class Exam { /** * *2014-1-10 下午9:43:48 by 孙振超 * *@param args *void * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub int studentNumber = 20; CountDownLatch countDownLatch = new CountDownLatch(studentNumber+1); DelayQueue< Student> students = new DelayQueue<Student>(); Random random = new Random(); for (int i = 0; i < studentNumber; i++) { students.put(new Student("student"+(i+1), 30+random.nextInt(120),countDownLatch)); } Thread teacherThread =new Thread(new Teacher(students)); students.put(new EndExam(students, 120,countDownLatch,teacherThread)); teacherThread.start(); countDownLatch.await(); System.out.println(" 考试时间到,全部交卷!"); } } class Student implements Runnable,Delayed{ private String name; private long workTime; private long submitTime; private boolean isForce = false; private CountDownLatch countDownLatch; public Student(){} public Student(String name,long workTime,CountDownLatch countDownLatch){ this.name = name; this.workTime = workTime; this.submitTime = TimeUnit.NANOSECONDS.convert(workTime, TimeUnit.NANOSECONDS)+System.nanoTime(); this.countDownLatch = countDownLatch; } @Override public int compareTo(Delayed o) { // TODO Auto-generated method stub if(o == null || ! (o instanceof Student)) return 1; if(o == this) return 0; Student s = (Student)o; if (this.workTime > s.workTime) { return 1; }else if (this.workTime == s.workTime) { return 0; }else { return -1; } } @Override public long getDelay(TimeUnit unit) { // TODO Auto-generated method stub return unit.convert(submitTime - System.nanoTime(), TimeUnit.NANOSECONDS); } @Override public void run() { // TODO Auto-generated method stub if (isForce) { System.out.println(name + " 交卷, 希望用时" + workTime + "分钟"+" ,实际用时 120分钟" ); }else { System.out.println(name + " 交卷, 希望用时" + workTime + "分钟"+" ,实际用时 "+workTime +" 分钟"); } countDownLatch.countDown(); } public boolean isForce() { return isForce; } public void setForce(boolean isForce) { this.isForce = isForce; } } class EndExam extends Student{ private DelayQueue<Student> students; private CountDownLatch countDownLatch; private Thread teacherThread; public EndExam(DelayQueue<Student> students, long workTime, CountDownLatch countDownLatch,Thread teacherThread) { super("强制收卷", workTime,countDownLatch); this.students = students; this.countDownLatch = countDownLatch; this.teacherThread = teacherThread; } @Override public void run() { // TODO Auto-generated method stub teacherThread.interrupt(); Student tmpStudent; for (Iterator<Student> iterator2 = students.iterator(); iterator2.hasNext();) { tmpStudent = iterator2.next(); tmpStudent.setForce(true); tmpStudent.run(); } countDownLatch.countDown(); } } class Teacher implements Runnable{ private DelayQueue<Student> students; public Teacher(DelayQueue<Student> students){ this.students = students; } @Override public void run() { // TODO Auto-generated method stub try { System.out.println(" test start"); while(!Thread.interrupted()){ students.take().run(); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }
2万+

被折叠的 条评论
为什么被折叠?



