java的缓存池_java任务缓存池,和消费过程

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    }   } } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值