用两种方式实现
第一种,使用timertask实现,timertask,可以比较精确的实现定时任务。
在这里插一句,java的实时性是很差的,timertask也就是大概的可以实现
看代码:
Producer.java
package me.dapeng.timer;
import java.util.concurrent.LinkedBlockingQueue;
public class Producer extends Thread {
private LinkedBlockingQueue queue;
public Producer(LinkedBlockingQueue queue) {
this.queue = queue;
}
@Override
public void run() {
int i = 0;
while (true) {
queue.offer("string" + i);
// System.err.println("[Producer]queue size:" + queue.size());
i++;
}
}
}
Consumer.java
package me.dapeng.timer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;
import java.util.concurrent.LinkedBlockingQueue;
public class Consumer extends TimerTask {
private LinkedBlockingQueue queue;
public Consumer(LinkedBlockingQueue queue) {
this.queue = queue;
}
@Override
public void run() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = formatter.format(new Date());
int size = queue.size();
try {
if (size > 0) {
String str = (String) queue.poll();
System.err.println(date + "-" + str);
} else {
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
TestPoll.java
package me.dapeng.timer;
import java.util.Timer;
import java.util.concurrent.LinkedBlockingQueue;
public class TestPoll {
private final static LinkedBlockingQueue queue = new LinkedBlockingQueue(1000);
public static void main(String[] args) {
Producer producer = new Producer(queue);
producer.start();
Consumer consumer = new Consumer(queue);
Timer timer = new Timer();
timer.schedule(consumer, 2000, 1000);
}
}
第二种方式
Producer.java
package me.dapeng.poll;
import java.util.concurrent.LinkedBlockingQueue;
public class Producer extends Thread {
private LinkedBlockingQueue queue;
public Producer(LinkedBlockingQueue queue) {
this.queue = queue;
}
@Override
public void run() {
int i = 0;
while (true) {
queue.offer("string" + i);
// System.err.println("[Producer]queue size:" + queue.size());
i++;
}
}
}
Consumer.java
package me.dapeng.poll;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
public class Consumer extends Thread {
private LinkedBlockingQueue queue;
public Consumer(LinkedBlockingQueue queue) {
this.queue = queue;
}
@Override
public void run() {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int i = 0;
while (true) {
String date = formatter.format(new Date());
int size = queue.size();
try {
if (size > 0) {
String str = (String) queue.poll(1, TimeUnit.SECONDS);
System.err.println(str);
} else {
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
}
}
}
TestPoll.java
package me.dapeng.poll;
import java.util.concurrent.LinkedBlockingQueue;
public class TestPoll {
private final static LinkedBlockingQueue queue = new LinkedBlockingQueue(1000);
public static void main(String[] args) {
Producer producer = new Producer(queue);
producer.start();
Consumer consumer = new Consumer(queue);
consumer.start();
}
}
_
其实这两种方式,都是同样的代码,仅仅是换用了timertask和while(true),重点还是用了queue,只是为了和我们老大证明queue.pool的使用方式而已。
_