File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

run method

 
Rahul Shilpakar
Ranch Hand
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33694
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand. Are you asking what Sun does when you call the run method or something else?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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:
 
Jigar Naik
Ranch Hand
Posts: 761
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic