• 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

run method

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How is the exact implementation of the run method? I mean how is the code of the run method in API.
 
author & internet detective
Posts: 41763
887
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand. Are you asking what Sun does when you call the run method or something else?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm also not sure what you're asking exactly. When you implement interface Runnable (or extend class Thread), you write the implementation of the run() method yourself.

If you want to know what the implementation of the run() method in class Thread looks like, then you can look at the source code of class Thread, which you can find in the file src.zip in the directory where you've installed the JDK. I looked it up, it looks like this:

So, it does this: If the Thread object has a target set (which is your Runnable object that you passed to the constructor), then it will call the run() method in the target. Otherwise it does nothing.
 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, i got it. i thought that it invokes the start( ) method. One more thing - Does start( ) method invokes run( ) method? If it runs whose (class) run( ) method it invokes? :roll:
 
Ranch Hand
Posts: 763
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<blockquote>code:
<pre name="code" class="core"> class BasicThread2 implements Runnable {
// This method is called when the thread runs
public void run() {
}
}

// Create the object with the run() method
Runnable runnable = new BasicThread2();

// Create the thread supplying it with the runnable object
Thread thread = new Thread(runnable);

// Start the thread
thread.start();
</pre>
</blockquote>


whose (class) run( ) method it invokes?



Object of Class will be assigned to Runnable (Target Object)

Runnable runnable = new BasicThread2();

runnable will be passed to Thread (Target runnable's target object)

Thread thread = new Thread(runnable);

Thread API

Ranchers correct me if i am wrong.
 
Sheriff
Posts: 22772
130
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The start method will call the Thread's own run() method. This in turn calls its Runnable target's run() method if there is a target, unless the Thread's run() method is overridden to perform a different action.

For instance,
<blockquote>code:
<pre name="code" class="core">class MyThread extends Thread
{
public void run()
{
System.out.println("Hello World!");
}

public static void main(String[] args)
{
Thread myThread = new MyThread();
myThread.start(); // calls myThread.run()
Thread regularThread = new Thread(myThread); // remember, Thread implements Runnable!
regularThread.start(); // calls myThread.run() indirectly
Thread emptyThread = new Thread();
emptyThread.start(); // does nothing since there is no target
}
}
</pre>
</blockquote>
[ July 15, 2008: Message edited by: Rob Prime ]
 
pie. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic