Running Thread by calling start() and run(), what is the difference?

后端 未结 7 1666
粉色の甜心
粉色の甜心 2020-12-07 04:00

It may be a basic question, i was confused with this,

in one file i have like this :

public class MyThread extends Thread {
    @Override
    public          


        
7条回答
  •  余生分开走
    2020-12-07 04:54

    start() runs the code in run() in a new thread. Calling run() directly does not execute run() in a new thread, but rather the thread run() was called from.

    If you call run() directly, you're not threading. Calling run() directly will block until whatever code in run() completes. start() creates a new thread, and since the code in run is running in that new thread, start() returns immediately. (Well, technically not immediately, but rather after it's done creating the new thread and kicking it off.)

    Also, you should be implementing runnable, not extending thread.

提交回复
热议问题