1

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?

River
  • 9
  • 2
  • Also, if no answers are satisfactory, please edit your question and explain in more detail what information you need. – markspace Aug 09 '20 at 14:34

1 Answers1

5

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.

markspace
  • 10,621
  • 3
  • 25
  • 39
  • 2
    Relevant though tangential Q&A: [“implements Runnable” vs “extends Thread” in Java](https://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread-in-java?). – Slaw Aug 08 '20 at 01:13