Java 单进程阻塞队列

Java 单进程阻塞队列,java,queue,message-queue,priority-queue,Java,Queue,Message Queue,Priority Queue,我正在编写一个与硬件通信的应用程序。虽然应用程序可以并行地同时接收和处理多个请求,但硬件不能 硬件要求这些并行请求基本上组织成一个线性请求链,每个请求一个接一个地执行 我还需要能够对请求进行优先级排序,因为其中一些是没有紧急情况的后台进程,而另一些是活动的,需要跳到队列的最前面进行立即处理 我对队列没有太多经验,但是如果这样的库还不存在,我会感到惊讶。参见 我建议对您的请求使用专门针对该队列的具有优先级值的包装器。例如,您可以在计算时使用该值 value = timestamp % N * pr

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

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

我还需要能够对请求进行优先级排序,因为其中一些是没有紧急情况的后台进程,而另一些是活动的,需要跳到队列的最前面进行立即处理

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

参见

我建议对您的请求使用专门针对该队列的具有优先级值的包装器。例如,您可以在计算时使用该值

value = timestamp % N * priorityLevel 
N取决于处理事件所需的时间

priorityLevel是一个值,越低表示越紧急(大于零)


编辑:在注释中的规范之后

似乎您需要创建 并将其传递给您自己的队列,该队列将是的实例。您放入此池的任务需要实现,该任务将按执行优先级排序

见位,但作为灵感应该足够了


编辑:建议的优先级函数对于较小的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) {}
    }
}

如果您已经熟悉,为什么不直接轮询它来处理硬件请求呢

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....
    }
}
公共类硬件管理员
公共静态最终优先级BlockingQueue队列=
新建PriorityBlockingQueue();
静止的{
while(true){
Message m=queue.take();
handleMessage(m);
}
}
私有静态无效handleMessage(消息m){
//处理消息。。。。
}
}

根据所有有用的注释和答案,我确定可能没有针对此问题的预构建解决方案。为了尝试为这个问题提供一个全面的答案,我尝试使用
PriorityBlockingQueue
编写自己的实现

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


编码一个有什么问题?并且,获取位于队列前面的单个进程/对象上的锁,该进程/对象将由硬件提供服务。@shekhar suman绝对没有。但在探索已经存在的东西之前,我不想重新发明轮子@tarka-我的意思是说并发API的存在是为了帮助您允许逐个访问硬件资源。请参阅,
Java并发API
,您已经在谈论使用优先级队列作为该
锁获取过程的队列。OP实际上已经知道优先级队列,他/她正在询问有关如何完成工作的库/方法。我不确定你的回答是否符合他/她的目的…@shekharsuman OP在哪里表明他熟悉
PriorityQueue
?@Kayaman检查问题是否标记有此
优先级队列
。这意味着OP已经知道了这一点。从问题的标签上看,OP显然应该能够找到它,但从问题本身看,他似乎并不真正知道。我知道但不熟悉
优先级队列
。我正在尝试确定这是否确实是适合这项工作的正确工具,以及是否有更好的/不同的/推荐的方法来解决这个问题,使用已经注意到的现有工具或库或陷阱。
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....
    }
}