在运行java程序时更改线程优先级?

时间:2021-04-26 02:19:34

I'm running a large multithreaded java job on a 64-core machine. The program has been running for days and I would like to change the priority of some threads created by java (not of the main thread), but without cancelling and restarting the program, as that would be a large waste of time and computing resources.

我正在64核计算机上运行一个大型多线程java作业。这个程序已经运行了好几天,我希望更改java创建的一些线程的优先级(不是主线程),但是不取消和重新启动这个程序,因为这将浪费大量的时间和计算资源。

Are there any ways to change thread priority at runtime, from the OS (linux)? I know the renice command in linux can renice the entire process, but I'm looking for a way to change the priority of the created threads at runtime (which doesn't seem to happen with just a renice).

有什么方法可以在运行时从操作系统(linux)更改线程优先级吗?我知道linux中的renice命令可以重命名整个进程,但是我正在寻找一种方法来改变在运行时创建的线程的优先级(这种情况似乎并不仅仅发生在renice上)。

1 个解决方案

#1


2  

It depends on your Java and Linux version but you can do this:

这取决于您的Java和Linux版本,但您可以这样做:

Make a thread dump of your application. First jps to find the process ID, then jstack for a thread dump.

创建应用程序的线程转储。首先jps查找进程ID,然后jstack查找线程转储。

The dump will contain nids:

转储将包含nids:

"GC task thread#0 (ParallelGC)" prio=10 tid=0x00007f322001f800 nid=0x6bd3 runnable

That's the PID of the thread in hex. You can use printf to convert it to decimal:

这是hex中线程的PID。您可以使用printf将其转换为decimal:

printf "%d\n" 0x6bd3

which gives us 27603.

这给了我们27603。

You can now use renice with this PID to change the thread priority.

您现在可以使用带有此PID的renice来更改线程优先级。

Sources:

来源:

#1


2  

It depends on your Java and Linux version but you can do this:

这取决于您的Java和Linux版本,但您可以这样做:

Make a thread dump of your application. First jps to find the process ID, then jstack for a thread dump.

创建应用程序的线程转储。首先jps查找进程ID,然后jstack查找线程转储。

The dump will contain nids:

转储将包含nids:

"GC task thread#0 (ParallelGC)" prio=10 tid=0x00007f322001f800 nid=0x6bd3 runnable

That's the PID of the thread in hex. You can use printf to convert it to decimal:

这是hex中线程的PID。您可以使用printf将其转换为decimal:

printf "%d\n" 0x6bd3

which gives us 27603.

这给了我们27603。

You can now use renice with this PID to change the thread priority.

您现在可以使用带有此PID的renice来更改线程优先级。

Sources:

来源: