Java Thread class run and start method differences

Source: Internet
Author: User
Tags thread class

public class Threadmodle {public static void main (string[] args) throws Interruptedexception {thread t = new Thread (New Ru Nnable () {@Overridepublic void run () {System.out.println ("Run ThreadID:" + thread.currentthread (). GetId ());}); System.out.println ("Main ThreadID:" + thread.currentthread (). GetId ());//t.run (); T.start (); T.join ();}}

  

1. Start ():

Let's take a look at the introduction of this method in the API:

Causes the thread to start executing, and the Java virtual machine invokes the thread's run method.

The result is two threads running concurrently, when the front thread (returned from the call to the start method) and another thread (executing its run method).

It is illegal to start a thread multiple times. This is especially the time when the thread has finished executing and cannot be restarted.

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 ():

Let's take a look at the introduction of the method in the API first:

If the thread is constructed using a stand-alone Runnable running object, the method of the object is called, Runnable run Otherwise the method does nothing and returns.

    ThreadSubclasses should override this method.

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.

Executing the above example, the run and start methods are executed separately, and you can see that the Run method runs the same thread ID as the main

Java Thread class run and start method 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.