DelayQueue Demo

DelayQueue怎么使用就不多少了,下面主要贴出一个例子。

例子中有三个类,分别是: StudentExamTask,ExamLauncher,Exam

StudentExamTask.java

package com.nothing;

import java.util.concurrent.Delayed;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author chenliu
 */
public class StudentExamTask implements Runnable, Delayed {
    private String name;
    private long submitTime;  // the time point when submit.  submitTime = currentTime + workingTime
    private long workingTime; // the time duration for exam
    private int i=0;

    public StudentExamTask(){}

    public StudentExamTask(String name, long submitTime) {
        this.name = name;
        workingTime = submitTime;
        this.submitTime = TimeUnit.NANOSECONDS.convert(submitTime, TimeUnit.MILLISECONDS) + System.nanoTime();
    }


    @Override
    public long getDelay(TimeUnit timeUnit) {
        System.out.println("round robin" + i++ + " ==> " + (submitTime - System.nanoTime()));
        return timeUnit.convert(submitTime - System.nanoTime(), TimeUnit.NANOSECONDS);
    }

    @Override
    public int compareTo(Delayed delayed) {
        StudentExamTask that = (StudentExamTask) delayed;
        return submitTime > that.submitTime ? 1 : (submitTime < that.submitTime ? -1 : 0);
    }

    @Override
    public void run() {
        System.out.println(name + " submit paper, working Time: " + workingTime / 100 + " minute");
    }


    public static class EndExamTask extends StudentExamTask {
        private ExecutorService exec;
        public EndExamTask(long submitTime, ExecutorService exec){
            super(null,submitTime);
            this.exec = exec;
        }

        public void run(){
            exec.shutdown();
        }
    }

}

ExamLauncher.java

package com.nothing;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.ExecutorService;

/**
 * @author chenliu
 */
public class ExamLauncher implements Runnable {
    private DelayQueue examTasks;
    private ExecutorService exec;

    public ExamLauncher(DelayQueue examTasks, ExecutorService exec) {
        this.examTasks = examTasks;
        this.exec = exec;
    }

    @Override
    public void run() {
        System.out.println("=====start exam======");
        while (!Thread.interrupted()) {
            try {
                examTasks.take().run();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("=====exam end=====");
    }
}


Exam.java

package com.nothing;

import java.util.Random;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author chenliu
 */
public class Exam {
    public static final int STUDENT_SIZE = 30;

    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();

        Random random = new Random();
        DelayQueue examTasks = new DelayQueue();
        for (int i = 1; i < STUDENT_SIZE + 1; i++) {
            int randomNumber = random.nextInt(9000);
            examTasks.put(new StudentExamTask("Student" + i, 3000 + randomNumber));
            System.out.println("Student" + i + " put time " + (3000 + randomNumber));
        }

        examTasks.put(new StudentExamTask.EndExamTask(12000, exec));
        exec.execute(new ExamLauncher(examTasks, exec));
    }
}


你可能感兴趣的:(JavaSE)