public static void main(String[] args) { Random r = new Random(); DelayQueue<Student> students = new DelayQueue<Student>(); ExecutorService exec = Executors.newCachedThreadPool(); for (int i = 0; i < STUDENT_SIZE; i++) { students.put(new Student("学生" + (i + 1), 3000 + r.nextInt(9000))); } students.put(new Student.EndExam(12000, exec)); // 1200为考试结束时间 exec.execute(new Teacher(students, exec)); }
/** all elements successfully put are contained */ public void testPut() { DelayQueue q = new DelayQueue(); for (int i = 0; i < SIZE; ++i) { PDelay x = new PDelay(i); q.put(x); assertTrue(q.contains(x)); } assertEquals(SIZE, q.size()); }
public static void main(String[] args) { Random rand = new Random(47); ExecutorService exec = Executors.newCachedThreadPool(); DelayQueue<DelayedTask> queue = new DelayQueue<DelayedTask>(); // Fill with tasks that have random delays: for (int i = 0; i < 20; i++) queue.put(new DelayedTask(rand.nextInt(5000))); // Set the stopping point queue.add(new DelayedTask.EndSentinel(5000, exec)); exec.execute(new DelayedTaskConsumer(queue)); }
public DelayedReply messageArrived(CellMessage envelope, PoolMgrGetUpdatedHandler message) { SerializablePoolManagerHandler handler = this.handler; UpdateRequest request = new UpdateRequest(envelope, message); requests.add(request); if (message.getVersion().equals(handler.getVersion())) { delays.put(request); UpdateRequest expired; while ((expired = delays.poll()) != null) { requests.remove(expired); } } else if (requests.remove(request)) { request.send(handler); } return request; }
public static void main(String[] args) throws InterruptedException { DelayQueue queue = new DelayQueue(); DelayedObj element1 = new DelayedObj("1", 100); DelayedObj element2 = new DelayedObj("0", 1000); queue.put(element1); queue.put(element2); DelayedObj relement1 = (DelayedObj) queue.take(); DelayedObj relement2 = (DelayedObj) queue.take(); System.out.println(relement1.getLevel()); System.out.println(relement2.getLevel()); }
@Benchmark public VElement testSimpleDelayQueue() throws InterruptedException { simpleDelayQueue.put(new VElement("message")); return simpleDelayQueue.take(); }
/** 请修改该方法,添加你的业务,修改参数,new */ public void delay() { // 10s System.out.println("wwww"); taskQueue.put(new LaogeDelayedTask(10 * 1000)); }
public void addEvent(Event c) { c.start(); q.put(c); }