Reputation: 759
Can we use directly run method as done in the below class. It is producing the same result as that when we use t1.start();. Is there any reason behind using only start method for invoking run?
public class runcheck extends Thread{
public void run(){
System.out.println(" i am run");
}
public static void main(String args[]){
runcheck as = new runcheck();
Thread t1 = new Thread(as);
t1.run();
}
}
Upvotes: 3
Views: 107
Reputation: 1336
From this:
start()
calls run()
asynchronously (non-blocking)
while calling run()
directly results in synchronous calling (blocking)
Upvotes: 1
Reputation: 11487
Yes, you can invoked run
directly from main
method, in this case the main
method and run
method runs one after the other.
Order
main -> t.run() -> main
- Only 1 Thread
But if you call start
method on the instance t1
, then the run
method and the main
method runs parallely.
main -> t.start() -> main
- 1st Thread
run()
- 2nd Thread.
Upvotes: 0
Reputation: 279930
Yes, but it will run in the same thread. It's equivalent to calling a method on a normal object.
start()
is what you want. It calls a native
method that actually spawns an OS Thread
to execute the run()
code in.
Upvotes: 8