Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some questions about Java Threads and Process Priorities

I have a small question about threads. On Unix systems, we have nice, which can be used to set priorities processes. OK, on ​​my system, I call some external processes, however, I would like to set priority for them. In unix, I could call other ProcessBuilder and set the nice to process I want, but in Windows, it is not possible.

If I start a thread with some priority, and use within ProcessBuilder it, the process will have the same priority as thread? Or is there some other way to do this?

Cheers

like image 674
caarlos0 Avatar asked Jun 20 '11 18:06

caarlos0


People also ask

What decides thread priority in Java?

In Java, a thread's priority is an integer in the range 1 to 10. The larger the integer, the higher the priority. The thread scheduler uses this integer from each thread to determine which one should be allowed to execute.

What will happen if two thread of the different priority?

Explanation: In cases where two or more thread with same priority are competing for CPU cycles, different operating system handle this situation differently. Some execute them in time sliced manner some depending on the thread they call. 5.


1 Answers

There's no way to set priority on a process (Process) level in Java.

If I start a thread with some priority, and use ProcessBuilder within it, the process will have the same priority as thread? Or is there some other way to do this?

The process will run side by side with the JVM, so it will not inherit the threads priority. It will be scheduled on it's own by the operating system.

As stated above, there is no built in cross-platform way of tweaking the priority of a process, but there is a Thread.setPriority(int) though. So perhaps you could do the work by the external program in a separate thread (instead of starting a new process) and use the setPriority method on this thread.

Related questions / answers:

  • Cross-platform way to change java process priority
  • How to change the priority of a running java process?
like image 78
aioobe Avatar answered Nov 15 '22 16:11

aioobe