0

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  
    }  
} 
Clad Clad
  • 2,653
  • 1
  • 20
  • 32
Parth
  • 568
  • 2
  • 6
  • 23

4 Answers4

2

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

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Still it shows me this kind of error "Could not find the main class: Multi." – Parth Mar 31 '14 at 10:35
  • How are you running the application? – Reimeus Mar 31 '14 at 10:36
  • Need more info here. Is the class in a package? What is the run command? – Reimeus Mar 31 '14 at 10:37
  • There's no such thing. I just picked up the program from a website, here http://www.javatpoint.com/what-if-we-call-run()-method-directly – Parth Mar 31 '14 at 10:39
  • Did you _compile_ the class before running it? – Reimeus Mar 31 '14 at 10:43
  • Allright...everything here is fair and good...I don't know why it didn't run earlier, but now when i tried running it on a different pc i worked out perfectly well. Strange!!! – Parth Mar 31 '14 at 11:22
  • you're welcome - on a related note, do read [`implements Runnable vs. extends Thread`](http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread) – Reimeus Mar 31 '14 at 11:35
1

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
GingerHead
  • 8,130
  • 15
  • 59
  • 93
1

Threads Are Started With Thread.Start()

And Look on Thread Life Cycle

enter image description here

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
0
t1.run();// just calls the run method (like any other method) in the same thread.

use t1.start() // starts a new thread.
TheLostMind
  • 35,966
  • 12
  • 68
  • 104