package queue;
import java.util.Queue;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentLinkedQueue;
public class test {
private Queue queue = new ConcurrentLinkedQueue();
public static void main(String[] args) throws Exception { test t = new test(); for(int i=0;i<100;i++){ t.insert("insert"+i); } t.ini(); } public void ini() { Timer timer = new Timer(); // sleep 1min, internal 1hour timer.schedule(new TimerDo(), 1 * 1000L, 5 * 1000L); // 一秒后开始 没5秒执行一次 // run中的内容 } public boolean insert(String str) throws Exception { System.out.println(str); queue.offer(str); if (queue.size() > 20000) { // do some thing } return true; } public class TimerDo extends TimerTask { @Override public void run() { try { String str = null; while (true) { if (queue.isEmpty()) { try { Thread.sleep(1000L); } catch (InterruptedException e) { e.printStackTrace(); } } else { str = queue.poll(); System.out.println("cosume=" + str); } } } catch (Exception e) { // do some thing } } } }