简体   繁体   中英

Simple thread program doesn't work

Hi, I'm a novice java programmer towards thread. And I'm stuck on this simple Java program

public class Multi extends Thread{  
    public void run() { 
        try{ 
            System.out.println("running...");
        }
        catch(Exception e){ 
            System.out.print(e);   
        }
    } 
    public static void main(String[] args){  
        Multi t1=new Multi();  
        t1.run();//fine, but does not start a separate call stack  
    }  
} 

Threads are started using the start method. Calling t1.run() method just synchronously executes the run method within the same Thread.

t1.start();

Read: Defining and Starting a Thread

Java threads are triggered by the following method:

t1.start() // This starts a new thread

While the following:

t1.run();// This calls the run method in the same thread

Threads Are Started With Thread.Start()

And Look on Thread Life Cycle

在此处输入图片说明

t1.run();// just calls the run method (like any other method) in the same thread.

use t1.start() // starts a new thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM