The difference between start () and run () in thread of the Java Basic Tutorial Java multithreading Tutorial _java

Source: Internet
Author: User
Tags thread class throwable

The thread class contains the start () and run () methods, what are the differences? This chapter will provide an answer to this. The contents of this chapter include:
Description of the difference between start () and run ()
Example of the difference between start () and run ()
Start () and run () related source (based on jdk1.7.0_40)

Description of the difference between start () and run ()
Start (): Its function is to start a new thread, the new thread will execute the corresponding run () method. Start () cannot be called repeatedly.
Run (): Run (), like a normal member method, can be called repeatedly. Calling run () alone executes run () in the current thread and does not start a new thread!

The following is explained in code.

Copy Code code as follows:

Class Mythread extends thread{
public void Run () {
...
}
};
Mythread mythread = new Mythread ();

Mythread.start () starts a new thread and runs the Run () method in the new thread.
Mythread.run () runs the Run () method directly in the current thread and does not start a new thread running run ().

Example of the difference between start () and run ()
Below, a simple example demonstrates the difference between them. The source code is as follows:

Copy Code code as follows:

Public synchronized void Start () {
Throw an exception if the thread is not in the ready state!
if (threadstatus!= 0)
throw new Illegalthreadstateexception ();

Add a thread to the Threadgroup
Group.add (this);

Boolean started = false;
try {
Start a thread by Start0 ()
Start0 ();
Set started tag
started = true;
finally {
try {
if (!started) {
Group.threadstartfailed (this);
}
catch (Throwable ignore) {
}
}
}

Run Result:

Copy Code code as follows:

Main call Mythread.run ()
Main is running
Main call Mythread.start ()
Mythread is running

Results show:
() Thread.CurrentThread (). GetName () is the name used to get the "current thread". The current thread is the thread that is dispatching execution in the CPU.
Mythread.run () is called in main thread main, and the Run () method runs directly on the main thread main.
() Mythread.start () starts thread Mythread, the Run () method is invoked when thread mythread is started, and the Run () method is run on thread mythread.

Start () and run () related source (based on jdk1.7.0_40)
The source code for the start () method in Thread.java is as follows:

Copy Code code as follows:

Public synchronized void Start () {
Throw an exception if the thread is not in the ready state!
if (threadstatus!= 0)
throw new Illegalthreadstateexception ();

Add a thread to the Threadgroup
Group.add (this);

Boolean started = false;
try {
Start a thread by Start0 ()
Start0 ();
Set started tag
started = true;
finally {
try {
if (!started) {
Group.threadstartfailed (this);
}
catch (Throwable ignore) {
}
}
}

Description: Start () actually starts the thread through the local method Start0 (). Start0 () will run a new thread, and the new thread will invoke the run () method.

Copy Code code as follows:

Private native void Start0 ();


The code for Run () in Thread.java is as follows:

Copy Code code as follows:

public void Run () {
if (target!= null) {
Target.run ();
}
}

Description: Target is a runnable object. Run () is the run () method that invokes the runnable member of the thread thread directly, and does not create a new thread.

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.