我有两个预定的任务。一个具有更高的优先级:必须最好地考虑其调度。第二个优先级较低:性能可能会影响其调度。
在ThreadGroup中,我们可以设置Priority。但是我不知道如何与执行器一起使用。
final ScheduledExecutorService serviceProduce = Executors.newScheduledThreadPool(2);
// May be delayed by performance
serviceProduce.scheduleAtFixedRate(new ComputeThread(), 0, 10, TimeUnit.MILLISECONDS);
// Should be kept as much as possible on schedule
serviceProduce.scheduleAtFixedRate(new ProduceOutputThread(), 0, 30, TimeUnit.MILLISECONDS);
答案 0 :(得分:0)
这是我解决的方式:
ThreadGroup threadGroupCompute = new ThreadGroup("compute");
threadGroupCompute.setMaxPriority(Thread.NORM_PRIORITY);
threadGroupCompute.setDaemon(false);
ThreadFactory threadFacoryCompute = new ThreadFactory() {
long cnt = 0;
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(threadGroupCompute, r, "thread-" + threadGroupCompute.getName() + "-" + cnt++);
return t;
}
};
final ScheduledExecutorService executorCompute = new ScheduledThreadPoolExecutor(1, threadFacoryCompute);
// May be delayed by performance
executorCompute .scheduleAtFixedRate(new ComputeThread(), 0, 10, TimeUnit.MILLISECONDS);
这2次。一个线程组的优先级设置为Thread.NORM_PRIORITY
,另一个线程组的优先级设置为Thread.MAX_PRIORITY