• 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

How thread's run method is working?

 
Greenhorn
Posts: 8
Eclipse IDE Java ME Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand the logic behind the code below. When i do not provide object of my class in Thread constructor run method is not working otherwise it does. How is it working?

class A implements Runnable {
public void run(){
System.out.println("This run method is without start");
}

public static void main(String[]args){
A obj = new A();
Thread t=new Thread(obj);
t.run();
}
}
 
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mr. Pandey wrote:I don't understand the logic behind the code below. When i do not provide object of my class in Thread constructor run method is not working otherwise it does. How is it working?

class A implements Runnable {
public void run(){
System.out.println("This run method is without start");
}

public static void main(String[]args){
A obj = new A();
Thread t=new Thread(obj);
t.run();
}
}



The Thread implementation of run() is as follows:



The member variable target is taken from the reference you pass in to the Thread constructor. If you use the no-arg constructor then target remains null and so run() returns with no side effects.

One very important thing to note here though. The code you posted does not run in a separate thread. In fact you have not created a new thread of execution at the operating system level there, you just have created an instance of a type called Thread. The separate thread of execution does not start until you call t.start(). The start() method runs some native code to start a new thread of execution, and then it calls the run() method from this new thread of execution. You should never call the run() method yourself, because that is exactly equivalent to not creating a Thread object and just calling run() on the runnable.
 
Mangal Pandey
Greenhorn
Posts: 8
Eclipse IDE Java ME Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is one more question in my mind that how start method call the run method internally ?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mr. Pandey wrote:There is one more question in my mind that how start method call the run method internally ?



That's not really a beginner question. The start() method calls native code outside of Java to use the operating system API to start a new thread. The newly created thread runs the run() method, and the original thread returns from the start() method and continues on its way. From this point on the two threads run independently of each other.

You don't need to know exactly how the new thread is started though, that's never important.
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike. J. Thompson wrote: . . . That's not really a beginner question. . . .

Agree. Moving thread.
 
Mangal Pandey
Greenhorn
Posts: 8
Eclipse IDE Java ME Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Mike. J. Thompson wrote: . . . That's not really a beginner question. . . .

Agree. Moving thread.



Thanks for reply..Which forum is appropriate for such questions. I am new here so need some suggestions.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mangal Pandey wrote:

Campbell Ritchie wrote:

Mike. J. Thompson wrote: . . . That's not really a beginner question. . . .

Agree. Moving thread.



Thanks for reply..Which forum is appropriate for such questions. I am new here so need some suggestions.



The thread has already been moved by Campbell. Check the breadcrumb at the top of this thread, you'll see it's not in the Threads an Synchronization forum. It's still visible in the Beginning Java forum too though.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic