The difference between the run () and start () of Java Thread

Source: Internet
Author: User
Tags terminates thread class

1. The start and run methods explain:1) Start:
Start the thread with the Start method, and actually implement multi-threaded operation, without waiting for the Run method body code to complete and proceed directly to execute the following code. by invoking the start () method of the thread class to start a thread, the thread is in a ready (operational) state and is not running, and once the CPU time slice is taken, the run () method is started.Here the method run () is called the thread body, which contains the contents of the thread to be executed, and the Run method ends and the thread terminates.
2) Run:
   The run () method is just a common method of the class, and if you call the Run method directly, there is still only one thread in the program ,The program execution path is only one, or sequential execution, or wait for the Run method body to complete before you can continue to execute the following code, so that the purpose of writing threads is not reached. Summary: Call the Start method to start the thread, and the Run method is just a normal method call to thread, or execute in the main thread. Both methods should be more familiar, put the code that needs to be processed in parallel in the run () method, and the start () method initiates the thread to call the run () method automatically, as specified by the JVM's memory mechanism. and the run () method must be public access and the return value type is void. 2. Supplemental notes to Java Threads:

Java threads are implemented through the Java.lang.Thread class. When the VM starts, it has a thread defined by the main method. You can create a new thread by creating an instance of the thread. Each thread completes its operation by means of the method run () that corresponds to a particular thread object, and the method run () is called the thread body. Start a thread by calling the start () method of the thread class.

In Java, threads typically have five states of creation, readiness, operation, blocking, and death.
The first is the creation state. In the build thread object, there is no call to the object's Start method, which is the thread in the created state.
The second is the ready state. When the start method of the thread object is called, the thread enters the ready state, but at this point the thread dispatcher has not set the thread to the current thread and is in a ready state. After a thread is running, it is ready to return from waiting or sleeping.
The third is the running state. The thread dispatcher sets the thread in the ready state to the current thread, and the thread enters the running state and starts running the code in the Run function.
The four is the blocking state. A thread is paused when it is running, usually in order to wait for a certain time to occur (for example, a resource is ready) before it continues to run. Methods such as sleep,suspend,wait can cause thread blocking.
Five is the state of death. If a thread's Run method finishes executing or calls the Stop method, the thread dies. For a thread that has died, the start method can no longer be used to get it ready.

There are two ways of implementing and starting a thread
1. Write a class that inherits from the thread class, overriding the Run method. Start a thread with the Start method
2, write a class to implement the Runnable interface, the implementation of the Run method. Start with the new Thread (Runnable target). Start () method

Multithreading principle :The equivalent of playing a game machine, only a game console (CPU), but there are a lot of people to play, so, start is queued! When the CPU is selected you are your turn, you run (), and when the CPU runs out of time slices, the thread continues to queue, waiting for the next run ().

When start () is called, the thread is placed in the waiting queue, waiting for the CPU to dispatch, not necessarily starting immediately, but putting the thread into a movable state. Then through the JVM, thread threads invokes the run () method, executing the thread body of this thread.Call run after start, so troublesome, in order not to call run directly? is to realize the advantages of multithreading, not this start.
1. Start () method to start the thread and really implement multi-threaded operation. Instead of waiting for the Run method body code to complete, you can proceed directly to the following code, starting a thread by calling the start () method of the thread class, which is in the ready state and not running. The thread class then calls the method Run () to complete its operation, where the method run () is called the thread body, it contains the contents of the thread to be executed, and the Run method ends, and it terminates. The CPU then dispatches other threads.
2. The Run () method is called as a normal method . The program is still executed sequentially, waiting for the Run method body to complete before the execution of the following code can continue, only the main thread-this one, the program execution path is only one, so that does not achieve the purpose of writing threads.

Remember : Multithreading is the use of time-sharing CPU, macro on all threads to execute together, also called concurrency.

3. Example
Package Com.threaddemo.rundemo;public class Rundemo {public static void main (string[] args) {System.out.println ("Main thread ID: "+ Thread.CurrentThread (). GetId ()); MyThread thread1 = new MyThread ("Thread1"); Thread1.start (); MyThread thread2 = new MyThread ("Thread2"); Thread2.run ();}} Class MyThread extends Thread {private String name;public MyThread (String name) {this.name = name;} @Overridepublic void Run () {System.out.println ("name:" + name + "Child thread ID:" + thread.currentthread (). GetId ());}}

Execution results are as follows

Main thread Id:1name:thread1 child thread id:11name:thread2 child thread Id:1

 

The following conclusions can be drawn from the output:

1) The thread IDs of Thread1 and Thread2 are different, and the thread2 and the main thread IDs are the same, stating that the Run method call does not create a new thread, but rather runs the run method directly in the main thread, without any difference from the normal method invocation;

2) Although the Thread1 start method call is called before the Thread2 Run method, the first output is information about the Thread2 Run method call, stating that the process created by the new thread does not block subsequent executions of the main thread.

The difference between the run () and start () of Java Thread

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.