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

后端 未结 7 1644
粉色の甜心
粉色の甜心 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:46

    In one line directly calling run() is synchronous (your code will block until run() returns) and calling start() (your code will not wait for run to complete as it is called in other thread obj) is asynchronous.

    When you directly use start() method, then the thread will run once using the Runnable instance you provided to it and then the thread will be unusable.

    But to leverage the Thread Pooling and Scheduling capabilities that are inbuilt in Java, extending a Runnable or Callable is the way to go.

提交回复
热议问题