Thread run, start, Threadrunstart

Source: Internet
Author: User

Thread run, start, Threadrunstart

Class TestThread extends Thread {

Public void run (){
For (int I = 0; I <20; I ++ ){
System. out. println (Thread. currentThread (). getName () + "--- is running ");
}
}
}
Public class Thread01 {
Public static void main (String [] args ){
TestThread tt1 = new TestThread ();
TestThread tt2 = new TestThread ();
// Tt1.run ();
// Tt2.run ();
Tt1.start ();
Tt2.start ();
// New TestThread (). start ();

}

}

The start () and run () methods in the Thread.

Start () is the focus of concurrency implementation, and run () is just to call the run () method in the TestThread class.

When start () is used, the result is:

Thread-0 --- is running
Thread-1 --- is running
Thread-0 --- is running
Thread-1 --- is running
Thread-0 --- is running
Thread-0 --- is running
Thread-1 --- is running
Thread-0 --- is running
Thread-1 --- is running
Thread-0 --- is running
Thread-1 --- is running
Thread-0 --- is running
Thread-1 --- is running
Thread-0 --- is running
Thread-1 --- is running
Thread-0 --- is running
Thread-1 --- is running
When run () is called, the result is:

Main --- is running
Main --- is running
Main --- is running
Main --- is running
Main --- is running
Main --- is running
Main --- is running
Main --- is running
Main --- is running
Main --- is running
Main --- is running
Main --- is running
Main --- is running
Main --- is running
Main --- is running
Main --- is running
Main --- is running
Main --- is running

Starting a new thread does not directly call the run method of the subclass, but calls the start method of the thread class. The start method of the thread class will generate a new thread, and run the run method in the thread class object on the new thread. Because the code segment of the thread is in the run method, the thread ends after the method is executed.


In java, what is the difference between thread start () and run?

I try my best to explain the problem clearly. Please give me some advice on the shortcomings!
First,
The reason why a thread occurs is to make better use of the CPU and make her work more "savvy.

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,

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.

Remember: The thread is used to make better use of the CPU,
Increase the program running speed!

Public class TestThread1 {
Public static void main (String [] args ){
Runner1 r = new Runner1 ();
// R. run (); // This is a method call, not a thread.
Thread t = new Thread (r); // call the Thread (Runnable target) method. The parent class object variable points to the subclass object.
T. start ();

For (int I = 0; I <100; I ++ ){
System. out. println ("entering Main Thread running state ");
System. out. println (I );
}
}
}
Class Runner1 implements Runnable {// after implementing this interface, jdk will know that this class is a thread
Public void run (){

For (int I = 0; I <100; I ++ ){
System. out. println ("Enter Runner1 running state ");
System. out. println (I );
}
}
}

If you still cannot understand it,
My QQ: 516258551
Let's discuss it on QQ.

I wonder if the answer will satisfy you.

In JAVA, what is the difference between the run () and start () Methods of thread? Confused me.

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.

For example:
Remember: The thread is used to make better use of the CPU,
Increase the program running speed!

Public class TestThread1 {
Public static void main (String [] args ){
Runner1 r = new Runner1 ();
// R. run (); // This is a method call, not a thread.
Thread t = new Thread (r); // call the Thread (Runnable target) method. The parent class object variable points to the subclass object.
T. start ();

For (int I = 0; I <100; I ++ ){
System. out. println ("entering Main Thread running state ");
System. out. println (I );
}
}
}
Class Runner1 implements Runnable {// after implementing this interface, jdk will know that this class is a thread
Public void run (){

For (int I = 0; I <100; I ++ ){
System. out. println ("Enter Runner1 running state ");
System. out. println (I );
}
}
}

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.