Java – the following code runs successfully. Does that mean we can start the thread twice?

The following code runs successfully. Does that mean we can start the thread twice?

public class enu extends Thread {
    static int count = 0;

    public void run(){
        System.out.println("running count "+count++);
    }

    public static void main(String[] args) {
        enu obj = new enu();
        obj.run();
        obj.start();
    }
}

Output – run count 0 run count 1

Solution

No, when you call obj When you start (), you start the new thread only once obj. Run() executes the run method in the current thread It does not create a new thread, you can call it as many times as needed

On the other hand, it is not possible to call obj multiple times start().

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>