user973758
user973758

Reputation: 727

Java priority workaround

I have a set of non-trivial methods and the result of one of these will eventually be requested. I want to run all of the methods in parallel so that the answer from whichever one is chosen will be immediately available.

I know the probability of each method being selected and so I would like to reflect these probabilities in the priorities of the methods as they run in parallel.

The OS I use ignores the priorities I give to threads and so is there a workaround to ensure that the thread running the method with the highest probability will get more CPU resources?

Upvotes: 1

Views: 98

Answers (2)

marecar
marecar

Reputation: 33

Maybe you could ignore thread priorities then, and use different ExecutorService-s for each separate method/e.g. resource consuming computation.For example, you could call

 Executors.newFixedThreadPool(int)

where int wolud reflect probability of a method being called (bigger the probability, more threads to be allocated). This of course means that work on every one of those methods should be done in parallel.

Upvotes: 1

akhilless
akhilless

Reputation: 3219

Thread class has setPriority method. But it might not be of great help because according to an article which discusses thread priority in Java and gets mentioned in this answer what priority really means is operating system dependent. In particular the article points out that

there's no relation per se between priority and CPU allocation

because each operating system has its own peculiarities in thread scheduling. For example, for thread priority to work on Linux the JVM should be running as root or with root privileges set via setuid (according to the same article).

Upvotes: 1

Related Questions