线程参数less构造函数的用途是什么?

huangapple go评论70阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年8月8日 08:42:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63310740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定