Java Thread multi-Thread start () and run ()

Source: Internet
Author: User

This article will introduce some simple examples and introductions about Java Thread multithreading start () and run (). If you are interested, please refer to it.

1. The start () method is used to start the thread and realize multi-threaded running. In this case, you do not need to wait until the execution of the run method body code is complete and directly continue to execute the following code:
 
Start a Thread by calling the start () method of the Thread class,
This thread is in the ready state,
Not running.
Then, the Thread class calls the method run () to complete its operation,
Here the run () method is called the thread body,
It contains the content of the thread to be executed,
The Run method is finished,
This thread is terminated,
The CPU then runs other threads,

2. The run () method is called as a normal method. The program must be executed sequentially, or the following code can continue to be executed after the run method is completed:
If you directly use the Run method,
This is just to call a method,
The program still has only the main thread-this thread,
There is only one program execution path,
In this way, the write thread is not achieved.
Implement multithreading through implements Runnable interface

Sample Code:

The Code is as follows: Copy code

Package com;

Public class TestRunnable implements Runnable {
Private int num = 0;
Public static void main (String [] args ){
For (int I = 0; I <5; I ++ ){
New Thread (new TestRunnable (I). start ();
}
}

Public TestRunnable (int num ){
This. num = num;
}

// Implementation Method
Public void run (){
System. out. println ("thread:" + this. num );
}
}

The results printed in the preceding example are unordered:
1 4 3 2 0

It may also be in another order. By calling start through the Thread class, multithreading is implemented, so you don't have to wait until the execution of the run method is complete before starting a new Thread.

If

The Code is as follows: Copy code

New Thread (new TestRunnable (I). start ()

Change

New Thread (new TestRunnable (I). run (),

The printed results are ordered, that is:

0 1 2 3 4


Directly calling the run method through the Thread class is actually calling the run method of the TestRunnable class, and viewing the run method of the Thread class:

The Code is as follows: Copy code

 


Public void run (){
If (target! = Null ){
Target. run ();
}
}

We found that the Thread class run method calls the run () of TestRunnable.

The Code is as follows: Copy code

New Thread (new TestRunnable (I). run ()

It is not a multi-thread. Its execution thread is still the main thread.

 

Next we will post the start method of the Thread class, which can be compared with the run method of the Thread class above:

The Code is as follows: Copy code

/**
* Causes this thread to begin execution; the Java Virtual Machine
* Callthe <code> run </code> method of this thread.
* <P>
* The result is that two threads are running concurrently:
* Current thread (which returns from the call to
* <Code> start </code> method) and the other thread (which executes its
* <Code> run </code> method ).
* <P>
* It is never legal to start a thread more than once.
* In particle, a thread may not be restarted once it has completed
* Execution.
*
* @ Exception IllegalThreadStateException if the thread was already
* Started.
* @ See # run ()
* @ See # stop ()
*/
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 ();

/* Sort y 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 */
}
}
}

Related Article

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.