Thread's start method and run Method

Source: Internet
Author: User

I used thread in my work. I started with an error and studied it carefully for a moment.

Preface: Today I wrote code like this.

new Thread() { @Overridepublic void run() {System.out.println("test");} }.run();


In this way, a new thread execution is started, but after printing the thread information, it is still found in the main thread. After careful consideration, is it true that I am doing this differently from calling the tostring method of a new object? There will be no new thread of Shenma.
Of course, our main implementation is in run, which is also the only method of implements runnable of thread.
So we call the start method to execute the processing logic in run in the new thread, so a start method calls the run method.
But that's not the case.
Start method code

/**     * Starts the new Thread of execution. The <code>run()</code> method of     * the receiver will be called by the receiver Thread itself (and not the     * Thread calling <code>start()</code>).     *     * @throws IllegalThreadStateException if the Thread has been started before     *     * @see Thread#run     */    public synchronized void start() {        if (hasBeenStarted) {            throw new IllegalThreadStateException("Thread already started."); // TODO Externalize?        }         hasBeenStarted = true;         VMThread.create(this, stackSize);    }

Please note that the above javadoc the code run () method of the handler er will be called by the handler thread itself (and not the thread calling start ())
Who is calling the run method so implicitly?
In fact, it is indirectly called by START. Start calls the create method of vmthread, while the system (the thread required for OS creation), and then the JVM calls the run method. JVM is called directly.
The following is a very geek article:

Start Method

Public void start ()-This method is responsible for causing the thread (it's called upon) to begin execution. the name may seem to be a misnomer here as this method only causes and not actually starts the execution. it just schedules the thread and when
CPU scheduler picks this thread for excution then the JVM callthe run () method to actually start the execution of the thread.

This method will obviusly result into two concurrent threads-one, from which this method is called and two, the new thread which executes the run () method of the new thread instance.

A thread will throw illegalstateexception in case you try to call the START () method on an already started thread instance. that means, a thread can not be started more than once. as per the Java specifications a thread may not be restarted after completing
Its execution. You'll be required to create and start the execution of a new thread in that case.

Run Method

Public void run () -This is the only method of the runnable interface and the classes which intend to execute their code in a separate thread of execution first implement the runnable interface and subsequently define this method and put all the code expected
To be executed in the separate thread inside the scope of this run method.

The other way, such classes can follow for the same is by extending the Thread class and in that case also they shocould oevrride the run method of the thread class and put all the code supposed to be executed in the new thread inside the scope of the overriden
Run method.

Difference between start () and run () Methods

Start () methods only schedules the thread for execution and not actually begins the execution of the thread. the execution of the thread is started when the JVM callthe run () method of the thread once the CPU scheduler picks this scheduled thread for execution.

Source: http://geekexplains.blogspot.com/2008/05/start-method-vs-run-method-in-java.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.