java - Single process blocking queue -
i writing application communicates hardware. while application can receive , process multiple requests simultaneously in parallel, hardware cannot!
the hardware requires these parallel requests organised linear request chain each 1 executed 1 after other.
i have requirement able prioritise requests given of background processes no urgency , live , need jumped front of queue immediate processing.
i don't have experience queues surprised if such library didn't exist.
see https://docs.oracle.com/javase/7/docs/api/java/util/priorityqueue.html
i advise using wrapper requests has priority value queue. instance, can use long value, calculate
value = timestamp % n * prioritylevel n dependent on how long takes process events
prioritylevel value lower means more urgent (bigger zero)
edit: after specification in comments
it seems need create instance of threadpoolexecutor , pass own queue instance of priorityblockingqueue. task put pool need implement comparable order them execution priority.
see bit old reference, inspiration should sufficient.
edit: suggested priority function dangerous smaller n, looking @ numbers, long can multiplied lot before overflow happen, leaving modulo out little , less, if have 2 priority levels (sorry mystification)
edit: implementation of suggested solution
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 1 thread (first 4 arguments) //using priority queue store tasks (it takes care of sorting 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 have terminated once test done executor.shutdown(); } } //in wrapper should loaded want have executed class eventwrapper implements comparable<eventwrapper>, runnable{ public final long priority; //name recognize being executed public final string name; public eventwrapper(int priority, string name){ //priority function out of current time, can inserted 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+" priority "+priority); //sleep rule out speed of insertion in executor try {thread.sleep(1000); } catch (interruptedexception ex) {} } } result created tasks is
executing task priority 1433276819484 executing task c priority 1433276819485 executing task e priority 1433276819485 executing task b priority 2866553638970 executing task d priority 4299830458455
Comments
Post a Comment