Java thread Start () and run () differences

Source: Internet
Author: User

1.start ()

public static void Main (string[] args) {//TODO auto-generated method stub thread t = new Thread () {public            void run () {                pong (); 
   
    }};        T.start ();        System.out.print ("ping");    } static void Pong () {System.out.print ("Pong");}}
   

Execution Result: pingpong

2.run ()

public static void Main (string[] args) {//TODO auto-generated method stub thread t = new Thread () {public            void run () {                pong (); 
   
    }};        T.run ();        System.out.print ("ping");    } static void Pong () {System.out.print ("Pong");}}
   

Execution Result: pongping

Cause: 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, where the method run () is called the thread body, which contains the contents of the thread to be executed. This thread terminates when the Run method finishes running.
2) Run:
The run () method is just a common method of the class, if you call the Run method directly, the program is still only the main thread of the threads, its program execution path is only one, or to execute sequentially, or to wait for the Run method body execution before you can continue to execute the following code, This will not achieve the purpose of writing threads. 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 familiar, put code that needs to be processed in parallel in the run () method, and the start () method will automatically call the run () method, which is prescribed by the JVM's memory mechanism. and the run () method must be public access, and the return value type is void:

Java thread Start () and run () differences

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.