英文:
What is the use of the thread parameterless constructor?
问题
如果线程构造函数有参数,并且参数是函数入口,它等同于创建一个新线程来执行此函数。
然而,线程也有一个无参构造函数,似乎不能将线程类的任何成员函数再次绑定到执行函数。
问题:默认情况下构造的线程对象有什么用?如何执行子线程?
英文:
If the thread constructor has parameters, and the parameter is a function entry, it is equivalent to creating a new thread to execute this function.
However, thread also has a parameterless constructor, and it seems that no member function of the thread class can be bound to an execution function again.
Question: What is the use of the thread object constructed by default? How can the child thread be executed?
答案1
得分: 5
当你询问为什么答案会充满困难。 不可能洞察设计语言的人的思维。 我来猜一下。
Thread
也允许子类化。 例如,这个线程类确实有一个实现了 run()
方法,并使用了无参数的构造函数。
public class HelloThread extends Thread {
@Override
public void run() {
System.out.println("Hello Thread.");
}
}
new HelloThread().start();
所以有多种方法可以让线程对象执行任意代码。 Runnable
参数并不总是必需的。
英文:
When you ask why answers are fraught. It's impossible to see into the mind of the person who designed the language. I'll take a guess though.
Thread
also allows subclassing. For example, this thread class does have an implemented run()
method, and uses the no-argument (nullity) constructor.
public class HelloThread extends Thread {
@Override
public void run() {
System.out.println( "Hello Thread." );
}
}
new HelloThread().start();
So there's more than one way to get a thread object to execute arbitrary code. The Runnable
argument isn't always needed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论