Run and start

Source: Internet
Author: User

  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

Run and start

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.