How to Start a Thread in Java: MyThread Example
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:
- Creates a new thread: The JVM allocates resources for the new thread.
- Calls the
run()method: Therun()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.
原文地址: https://www.cveoy.top/t/topic/odTQ 著作权归作者所有。请勿转载和采集!