如何安排Java线程 [英] How to schedule Java Threads

查看:113
本文介绍了如何安排Java线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过Java线程是用户级线程,用户级线程和内核级线程之间的区别之一是内核级别线程由内核调度(我们无法更改),对于用户级线程我们可以定义我们自己的调度算法。

I have read that Java threads are user-level threads and one of the differences between user level threads and kernel level threads is that kernel level threads are scheduled by the kernel(we cannot change it) where as for user level threads we can define our own scheduling algorithm.

那么我们如何在Java中调度线程?在任何给定时间,当准备好执行多个线程时,运行时系统会选择具有最高优先级的 Runnable 线程来执行。如果具有相同优先级的两个线程正在等待CPU,则调度程序选择其中一个以循环方式运行。如果我不想要RR怎么办?有没有办法可以改变它或者我在这里遗漏了什么?

So how do we schedule threads in Java? At any given time, when multiple threads are ready to be executed, the runtime system chooses the Runnable thread with the highest priority for execution. If two threads of the same priority are waiting for the CPU, the scheduler chooses one of them to run in a round-robin fashion. What if I don't want RR? is there a way I can change it or am I missing something here?

推荐答案

你不能像JVM那样改变调度算法这超出了范围。 JVM使用底层操作系统提供的用户线程的线程。

You cannot change the scheduling algorithm as for the JVM this is outside the scope. The JVM uses the threading of user threads provided by the underlying OS.

因此,从Java的角度来看,您无法更改调度算法。调度是自动完成的。

So from the Java perspective you cannot change the scheduling algorithm. The scheduling is done automatically.

Java中唯一可以做的就是设置线程的优先级。但是这没有定义如何影响调度算法。

The only thing in Java you can do is set the priority of the thread. But how this affects the scheduling algorithm is not defined.

您可以尝试更改运行VM的操作系统的调度算法。但这在很大程度上取决于使用的操作系统。

You can try to change the scheduling algorithm of the OS where your VM is running on. But this is highly dependend on the OS used.

这篇关于如何安排Java线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆