Java thread Run and start

Source: Internet
Author: User

Inheriting the thread in Java, there are two methods for thread initiation: Start () and run (). Here's a brief look at the difference between the two.

Start (): Starts a thread, at which point the thread is in a ready state, then calls the run () method of the Thread object, and cannot start a thread more than once. After the main method execution ends, the thread created by the start () method does not end up running, so the main thread fails to exit until the thread threads are executed. Note that the default thread created is the user thread (not the daemon thread). The java.lang.IllegalThreadStateException exception occurs when the start () method is called multiple times.

Run (): invokes the Run () method of the thread object within this thread; it can be repeated multiple times (because only one method is called). The program is still only the main thread of this line.

Here is the test code:

 PackageMutithread; Public classExtendthreadextendsThread {PrivateString name;  PublicExtendthread () {} PublicExtendthread (String name) { This. Name =name; }         Public voidrun () {inti = 0;  while(I < 2) {System.out.println ("Thread" + This. Name + "is running" +i); I++; }            }         Public Static voidMain (String args[]) {Extendthread Et1=NewExtendthread ("A"); //First Kind//Description: The call to run () is not any different from the other method, the main method executes it sequentially and prints the last sentence. //possible result: Thread A is running 0//Thread A is running 1//main thread is over//Et1.run (); //Et1.run ();//no exception, equivalent to two times the run () method was called//The second Kind//Description: The Start () method re-creates a thread, and after the main method execution ends, the thread created by the start () method does not run to the end,//therefore the main thread could not exit until thread threads are executed. Note that the default thread created is the user thread (not the daemon thread)//possible result: Thread A is running 0//Thread A is running 1//main thread is over//or: Main thread is over//Thread A is running 0//Thread A is running 1//Et1.start (); //Et1.start ();//Java.lang.IllegalThreadStateException exception at second boot. //Third Kind//1, why did not print out 4 sentences? Because we set the thread thread to Daemon (daemon) thread, it is possible to exit the program only when the daemon thread exists, so only seven sentences are printed out.//2. When the Java virtual machine has a daemon thread running, the Java virtual opportunity is turned off. When all regular threads have finished running,//the virtual machine exits running regardless of where the daemon is running. So it's best that your daemon doesn't write business logic that will affect the program. Otherwise, you can't predict what's going to happen to the program.//possible result: main thread is over//Thread A is running 0//or: Main thread is over//Et1.setdaemon (TRUE); //Et1.start (); //Fourth Type//user thread can be forced to kill by System.exit (1)//possible result: main thread is over//Thread A is running 0//Thread A is running 1//or main thread is overEt1.start (); System.out.println ("Main thread is over"); System.exit (1); }}

Reference: http://www.cnblogs.com/linjiqin/archive/2011/04/10/2011272.html

Java thread 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.