Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Callin run()  RSS feed

 
Rizwan Qadri
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, All
Can we call run() directly in thread? I am calling this and it is giving me right output. I am confused.

class Hello extends Thread
{
public static void main(String[] args)
{
Hello hl=new Hello();
Thread t=new Thread(hl);
t.run();
System.out.println("Hello World!");
}
public void run()
{
System.out.println("In Run");
}
}

Output--In Run
Hello world
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It may produce the correct output, but what is actually happening is very different from calling start - calling start will actually start a new thread of execution, while calling run will execute the code in the current thread (i.e., no new thread is created).
 
Rizwan Qadri
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am still confused,then why we call start() and not run() in our program when result is same.
 
abhishek pendkay
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can call run() directly but if you do that your run method will not execute in a seprate thread it will just execute in the thread from which you called it....
for eg in your program when you call t.run() run will be executed in the main thread (ie the thread calling your main method) and not in a seprate thread.... whereas calling t.start() will make run() execute in a seprate thread with its own seprate call stack
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why we call start() and not run() in our program when result is same.

The result is not the same. In this trivial example the same output may be produced, but as I said before, what happens internally in the JVM is very different.

Often threads are used to start some computation that takes a while, and which should not hold up the main thread if execution. Imagine your code was part of a GUI, and the run method took 5 minutes to finish - if you had called "run", the GUI would become unresponsive for 5 minutes - very undesirable. Only by calling "start" is the run method executed in its own thread, and thus the remainder of the code can continue to run.

I suggest you work through the concurrency chapter of the Java tutorial, particularly the Thread Objects section. It will make things clearer.
 
Henry Wong
author
Sheriff
Posts: 22846
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rizwan Qadri:
I am still confused,then why we call start() and not run() in our program when result is same.


Let's modify your code slightly.



The program now prints the statements in a loop forever -- pausing 1 second in between printouts -- instead of just printing a single message each.

Try it with run() and start() and see if there is any difference.

Henry
 
Rizwan Qadri
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Abhishek and Ulf
 
Rizwan Qadri
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!