rocking
rocking

Reputation: 4899

How Thread run starts?

I was looking a small example on Threads.For creating Threads we can do in 2 ways either by implementing Runnable interface or by extending Thread.I used the 1st way

package test;

public class test implements Runnable{
    public static void main(String args[])
    {
        test t=new test();
        t.run();Thread th=Thread.currentThread();
        th.start();
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("hi");
    }
}

My doubt is when we are calling th.start(); then run() is called.I want to know how.I thought internally there start() may be calling run() so I looked in the documentation of Thread class

The following is the start() declaration in Thread class

public synchronized void start() {
    /**
     * This method is not invoked for the main method thread or "system"
     * group threads created/set up by the VM. Any new functionality added
     * to this method in the future may have to also be added to the VM.
     *
     * A zero status value corresponds to state "NEW".
     */
    if (threadStatus != 0)
        throw new IllegalThreadStateException();

    /* Notify the group that this thread is about to be started
     * so that it can be added to the group's list of threads
     * and the group's unstarted count can be decremented. */
    group.add(this);

    boolean started = false;
    try {
        start0();
        started = true;
    } finally {
        try {
            if (!started) {
                group.threadStartFailed(this);
            }
        } catch (Throwable ignore) {
            /* do nothing. If start0 threw a Throwable then
              it will be passed up the call stack */
        }
    }
}

As you can see inside start(),run() is not called but when we are calling th.start() then automatically overriden run() is called.Can anybody please throw some light in this

Upvotes: 8

Views: 789

Answers (5)

Rishi Dwivedi
Rishi Dwivedi

Reputation: 928

 Thread th=Thread.currentThread();
    th.start();// its call run method automatically

 if you call direct run method with class object than its treat as a normal method not thread method

start() method of Thread class is used to start a newly created thread. It performs following tasks: A new thread starts(with new callstack). The thread moves from New state to the Runnable state. When the thread gets a chance to execute, its target run() method will run.

Upvotes: 1

Marko Topolnik
Marko Topolnik

Reputation: 200148

The mechanism whereby the run method is invoked on a new thread is extralinguistic: it cannot be represented in terms of Java code. This is the crucial line in the start method:

    start0();

start0 is a native method whose invocation will:

  • cause a new native thread-of-execution to be created;
  • cause the run method to be invoked on that thread.

Upvotes: 13

Vishwanath gowda k
Vishwanath gowda k

Reputation: 1695

Internally start method calls the run method.

Simple experiment may reveal the fact. Call Thread.start() and Thread.run(),, keep a print message statement inside run method. This message will be shown two times because the run method is called two times.

Check this statements in your question code. started flag was false before calling start0, after calling start0() function starrted flag is made true. it means the run method is called withing start0().

boolean started = false;

try {
    start0();
    started = true;
}

To be precise, the start0 method creates new thred to execute the run method and it returns the current thread.

From oracle official document

" public void start() Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

Throws: IllegalThreadStateException - if the thread was already started. See Also: run(), stop() "

Upvotes: 0

Jakub H
Jakub H

Reputation: 2158

You can find your answer in documentation of Thread.start() method. http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start()

Upvotes: 0

Niels Bech Nielsen
Niels Bech Nielsen

Reputation: 4859

It is not supposed to call the run method. If it does, it will do it in the same running thread.

Just like a program starts in the main method of a class, a new thread starts in the run method of the class. You have to look into native code to find it.

Upvotes: 0

Related Questions