0

I've got two scheduled tasks. One has the higher priority : its scheduling must be respected the best. The second one has a lower priority : the performances may more impact its scheduling.

In ThreadGroup we can the Priority. But I don't find how to this with Executors.

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); 
lvr123
  • 524
  • 6
  • 24

1 Answers1

0

This how I solved it:

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);

And this 2 times. One with a ThreadGroup with priority set to Thread.NORM_PRIORITY, one with priority set to Thread.MAX_PRIORITY

lvr123
  • 524
  • 6
  • 24