Java multi-Thread series-difference between start () and run () in Thread of "basic" 03

Source: Internet
Author: User

The difference between start () and run () indicates that start () is used to start a new thread and the new thread will execute the corresponding run () method. Start () cannot be called repeatedly. Run (): run () is the same as a common member method and can be called repeatedly. If run () is called separately, run () will be executed in the current thread, rather than starting a new thread! The following code is used for illustration. Class MyThread extends Thread {public void run (){...}}; myThread mythread = new MyThread (); mythread. start () starts a new thread and runs the run () method in the new thread. Mythread. run () runs the run () method directly in the current thread, and does not start a new thread to run (). The difference between start () and run () is shown in the following simple example. Source code: Copy code 1 // Demo. java source code 2 class MyThread extends Thread {3 public MyThread (String name) {4 super (name); 5} 6 7 public void run () {8 System. out. println (Thread. currentThread (). getName () + "is running"); 9} 10}; 11 12 public class Demo {13 public static void main (String [] args) {14 Thread mythread = new MyThread ("mythread"); 15 16 System. out. println (Thread. currentThread (). getName () + "call myth Read. run () "); 17 mythread. run (); 18 19 System. out. println (Thread. currentThread (). getName () + "call mythread. start () "); 20 mythread. start (); 21} 22} copies the code running result: main call mythread. run () main is runningmain call mythread. start () mythread is running result Description: (01) Thread. currentThread (). getName () is the name used to obtain the "current thread. The current thread is the thread that is being scheduled and executed in the cpu. (02) mythread. run () is called in "main thread main". The run () method runs directly on "main thread main. (03) mythread. start () starts "thread mythread". After "thread mythread" is started, the run () method is called. At this time, the run () method runs on "thread mythread. Source Code related to start () and run () (based on JDK1.7.0 _ 40) Thread. the source code of the start () method in java is as follows: copy the code public synchronized void start () {// If the thread is not "ready", an exception is thrown! If (threadStatus! = 0) throw new IllegalThreadStateException (); // Add the thread to the ThreadGroup group. add (this); boolean started = false; try {// start the thread start0 () through start0 (); // set started to Mark started = true ;} finally {try {if (! Started) {group. threadStartFailed (this) ;}} catch (Throwable ignore) {}} copy the Code Description: start () actually starts the thread through the local method start0. Start0 () will run a new thread, and the new thread will call the run () method. Private native void start0 (); the Code for run () in Thread. java is as follows: public void run () {if (target! = Null) {target. run () ;}note: target is a Runnable object. Run () is the run () method that directly calls the Runnable Member of the Thread, and does not create a new Thread.

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.