• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Callin run()

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am still confused,then why we call start() and not run() in our program when result is same.
 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Abhishek and Ulf
 
Rizwan Qadri
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic