The difference between start () and run () in a thread

Source: Internet
Author: User
Tags instance method

Each thread has a task to perform. The thread's task-handling logic can be directly implemented in the run instance method of the tread class or invoked through this method, so

Run () is an ingress method that is equivalent to the thread's task-handling logic, which is called directly by the Java Virtual Machine when it runs the appropriate thread, rather than by the application code.

The function of Start () is to start the corresponding thread. Starting a thread is actually asking the Java virtual machine to run the appropriate thread, and when that thread is able to run is determined by the thread scheduler. the end of the start () call does not mean that the corresponding thread has started running, that the thread may run later, or it may never run.

Let's look at an example:

1 /**2 * Run () an entry method equivalent to the thread's task handling logic3 * Start () is the function of starting the corresponding thread4  * @authorYirain5  *6  */7  Public classStartandrun {8      Public Static voidMain (string[] args) {9Thread T =NewThread () {Ten              Public voidrun () { One World (); A             } -         }; -          the T.start (); -         //T.run (); -System.out.print ("Hello"); -     } +      -     Static voidWorld () { +System.out.print ("World"); A     } at  -}

Output Result:

The corresponding call to T.run (), just a method of invoking an instance, does not start the thread, so run () is immediately executed

Output Result:

The difference between start () and run () in a thread

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.