The correct way to start the MyThread thread is:

public class MyThread extends Thread {

    public void run(){...}

}

A. new MyThread().start();

B. new MyThread().run();

C. None of the above.

D. new MyThread().sleep()

Answer: A. new MyThread().start();

Explanation:

The start() method is the correct way to initiate a thread in Java. It does the following:

  1. Creates a new thread: The JVM allocates resources for the new thread.
  2. Calls the run() method: The run() method, which contains the thread's execution logic, is invoked by the newly created thread.

Calling run() directly within the main thread will simply execute the run() method's code sequentially, without creating a new thread. Therefore, option A is the correct way to start the MyThread thread.

How to Start a Thread in Java: MyThread Example

原文地址: https://www.cveoy.top/t/topic/odTQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录