java 线程调度器注册?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7144018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thread scheduler registration?
提问by Nikhil
In java, does run()
register a thread in a thread scheduler?
在java中,是否run()
在线程调度程序中注册了一个线程?
What about construct()
,start()
and register()
?
怎么样construct()
,start()
和register()
?
采纳答案by Stephen C
In java, does run() register a thread in a thread scheduler?
在 java 中,run() 是否在线程调度程序中注册了一个线程?
No. If you call the run()
method directly, it is called as a normal method; i.e. it runs on the current thread, not a new one.
不会。如果run()
直接调用该方法,则作为普通方法调用;即它在当前线程上运行,而不是一个新线程。
What about construct(),start() and register()
构造(),开始()和注册()怎么样
The start
method creates a new thread, and in the process the thread will be registered with the scheduler. (However, the scheduler is a nebulous concept in Java. It is implied that one must exist, but its implementation and behavior are typically left to the host operating system. A pure Java program has almost no control over the way that the thread scheduler actually works.)
该start
方法创建一个新线程,并在此过程中将线程注册到调度程序中。(然而,调度器在 Java 中是一个模糊的概念。它暗示必须存在,但它的实现和行为通常留给主机操作系统。纯 Java 程序几乎无法控制线程调度器实际上的方式作品。)
There are no construct()
or register()
methods in the Thread
API. If you are referring to the Thread
constructors, they only create a Thread
object, and NOT the underlying thread that will do the work. The latter is only created when start()
is called.
API中没有construct()
orregister()
方法Thread
。如果您指的是Thread
构造函数,它们只会创建一个Thread
对象,而不是将执行工作的底层线程。后者仅在start()
被调用时创建。
回答by Owen
run()
is the actual code in the thread; so you could do like:
run()
是线程中的实际代码;所以你可以这样做:
Thread childThread = new Thread() {
public void run() {
// do stuff on a new thread
}
};
(Though I've been told extending Thread like that is ugly ;)
(虽然有人告诉我像那样扩展 Thread 是丑陋的;)
So calling run() itself won't create a new thread. To do that, you use start():
所以调用 run() 本身不会创建一个新线程。为此,您可以使用 start():
childThread.start();
So, I guess it does give the scheduler a new thread to deal with -- but that's way down on the OS level.
所以,我想它确实给了调度程序一个新的线程来处理——但这在操作系统级别上是低的。
I'm not sure what you mean by construct()
and register()
though?
我不知道你的意思construct()
,并register()
有关系吗?