Differences between start () and run () in Java Multi-threading

Source: Internet
Author: User
Tags thread class

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. When a thread is running, it is paused, usually in order to wait for an event 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 a thread and really implement multi-threaded running. 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.
The 2.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

Package Basic; Public classThreadTest { Public Static voidMain (string[] args) {Runner1 runner1=NewRunner1 (); Runner2 Runner2=NewRunner2 (); //thread (Runnable target) assigns a new thread object. Thread Thread1 =NewThread (Runner1); Thread thread2=NewThread (RUNNER2); Thread1.start (); //performing Start,thread1 and Thread2 cross-executionThread2.start (); //Thread1.run ();//execution run,thread1 and thread2 sequential execution//Thread2.run ();    }}classRunner1 implements Runnable {//implementing the Runnable interface, the JDK knows that this class is a thread     Public voidrun () { for(inti =0; I < -; i++) {System. out. println ("Enter the Runner1 running state ——————————"+i); }    }}classRunner2 implements Runnable {//implementing the Runnable interface, the JDK knows that this class is a thread     Public voidrun () { for(inti =0; I < -; i++) {System. out. println ("Enter Runner2 running state =========="+i); }    }}

Differences between start () and run () in Java Multi-threading

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.