package com.yj.task;
/**
* Created by jesse on 17/12/5.
*/
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import com.google.common.collect.Queues;
public class T {
public static void main(String[] args) {
final BlockingQueue<Long> q = new LinkedBlockingQueue<Long>();
new Thread(new Runnable(){
public void run() {
long num=1L;
while(true)
{
try {
System.out.println(num);
q.put(num);
num++;
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}}).start();
while (true) {
try {
List<Long> l = new ArrayList<Long>(10);
Queues.drain(q, l, 1000, 3, TimeUnit.HOURS);
//q.drainTo(l,10);
System.out.println(l);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Queues.drain(q, l, 1000, 3, TimeUnit.HOURS);
从l中取1000条数据,如果没有取到1000条会一直取,直到3小时后才会做后续操作
队列queues的方法drainTo没有阻塞功能,如取10条数据,但是队列中数据不过,那只会取有限的数据