单进程阻塞队列

时间:2015-05-29 11:05:41

标签: java queue message-queue priority-queue

我正在编写一个与硬件通信的应用程序。虽然应用程序可以并行同时接收和处理多个请求,但硬件不能!

硬件要求这些并行请求基本上被组织成一个线性请求链,每个请求链一个接一个地执行。

我还要求能够优先处理请求,因为某些后台进程没有紧急程度,有些是实时的,需要跳转到队列的前面以​​便立即处理。

我对队列没有多少经验,但是如果这样的图书馆还不存在,我会感到惊讶。

3 个答案:

答案 0 :(得分:0)

请参阅https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html

我建议使用包装器来为您的请求专门为此队列设置优先级值。例如,您可以使用Long作为计算

的值
value = timestamp % N * priorityLevel 

N取决于您处理事件所需的时间

priorityLevel是值,其中lower表示更紧急(大于零)

编辑:在评论中指定后

似乎你需要创建一个实例 ThreadPoolExecutor 并将其传递给您自己的队列,该队列将是PriorityBlockingQueue的实例。您放入此池的任务需要实现Comparable,它将按执行优先级对它们进行排序。

old reference位,但灵感应该足够了。

编辑:建议优先级函数对于较小的N是危险的,现在查看数字,long可以在溢出发生之前成倍增加,因此将模数输出将会做的越来越少,尤其是如果你只有两个优先级(抱歉神秘化)

编辑:实施建议的解决方案

import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class QTest {
    public static void main(String[] args){
        //create executor with exactly one thread (first four arguments) that is
        //using priority queue to store tasks (it takes care of sorting by priority)
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS, new PriorityBlockingQueue());
        executor.execute(new EventWrapper(1, "A"));
        executor.execute(new EventWrapper(2, "B"));
        executor.execute(new EventWrapper(1, "C"));
        executor.execute(new EventWrapper(3, "D"));
        executor.execute(new EventWrapper(1, "E"));
        //just to have it terminated once test is done
        executor.shutdown();
    }
}

//in this wrapper should be loaded anything you want to have executed
class EventWrapper implements Comparable<EventWrapper>, Runnable{
    public final long priority;
    //name just to recognize what is being executed
    public final String name;
    public EventWrapper(int priority, String name){
        //priority function out of current time, can be obviously inserted from elsewhere
        this.priority = priority*System.currentTimeMillis();
        this.name = name;
    }

    @Override
    public int compareTo(EventWrapper that) {
        //lower priority first
        if(this.priority==that.priority)return 0;
        return this.priority>that.priority?1:-1;
    }

    @Override
    public void run() {
        System.out.println("Executing task "+name+" with priority "+priority);
        //sleep to rule out speed of insertion in executor
        try {Thread.sleep(1000);
        } catch (InterruptedException ex) {}
    }
}

创建任务的结果是

Executing task A with priority 1433276819484
Executing task C with priority 1433276819485
Executing task E with priority 1433276819485
Executing task B with priority 2866553638970
Executing task D with priority 4299830458455

答案 1 :(得分:0)

如果您已熟悉PriorityBlockingQueue,为什么不简单地轮询它以处理硬件请求?

public class HardwareHandler

    public static final PriorityBlockingQueue<Message> queue = 
        new PriorityBlockingQueue<Message>();

    static {
        while (true) {
            Message m = queue.take();
            handleMessage(m);
        }
    }

    private static void handleMessage(Message m) {
        // handle message....
    }
}

答案 2 :(得分:0)

根据所有有用的评论和答案,我确定可能没有预先构建的解决方案。为了尝试提供一个全面的答案,我已经使用PriorityBlockingQueue编写了我自己的实现。

我在StackExchange Code Review上发布了代码,您可以看到完整代码和任何社区建议的改进。

See answer on code review