aspose file tools*
The moose likes Beginning Java and the fly likes run method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "run method" Watch "run method" New topic
Author

run method

Rahul Shilpakar
Ranch Hand

Joined: Aug 29, 2006
Posts: 132
How is the exact implementation of the run method? I mean how is the code of the run method in API.


Perform for today. Adapt for tomorrow.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30546
    
150

I don't understand. Are you asking what Sun does when you call the run method or something else?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14156
    
  19

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rahul Shilpakar
Ranch Hand

Joined: Aug 29, 2006
Posts: 132
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

Joined: Dec 12, 2006
Posts: 757
<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.


Jigar Naik


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: run method