This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
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

Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "run method" Watch "run method" New topic

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
author & internet detective

Joined: May 26, 2003
Posts: 30795

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: 14279

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 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 8 API documentation
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
<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

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

Joined: Oct 27, 2005
Posts: 19726

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,
<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
Thread regularThread = new Thread(myThread); // remember, Thread implements Runnable!
regularThread.start(); // calls indirectly
Thread emptyThread = new Thread();
emptyThread.start(); // does nothing since there is no target
[ July 15, 2008: Message edited by: Rob Prime ]

How To Ask Questions How To Answer Questions
wood burning stoves
subject: run method