Java Threading Summary (i)

Source: Internet
Author: User

First, a simple threading instance is pasted first:

 Public classMyThreadextendsthread{@Override Public voidrun () {Try {             for(inti = 0; I < 10; i++){                intTime = 1000;                Thread.Sleep (time); System.out.println ("Run =" +Thread.CurrentThread (). GetName ()); }        }Catch(interruptedexception e) {e.printstacktrace (); }    }     Public Static voidMain (string[] args) {MyThread MyThread=NewMyThread (); Mythread.setname ("MyThread");        Mythread.start (); Try{             for(inti = 0; I < 10; i++){                intTime = 1000;                Thread.Sleep (time); System.out.println ("main =" +Thread.CurrentThread (). GetName ()); }        }Catch(interruptedexception e) {e.printstacktrace (); }    }}

The results of the operation are as follows:

As can be seen from the running results, there are two threads in the program, one is the main thread, the other is the threads I created manually, and the main thread is created by the JVM.

The thread execution start () method does not represent the boot order of the thread, as in the following example:

 Public classMyThreadextendsthread{Private inti;  PublicMyThread (inti) {        Super();  This. i =i; } @Override Public voidrun () {System.out.println (i); }     Public Static voidMain (string[] args) {MyThread thread1=NewMyThread (1); MyThread thread2=NewMyThread (2); MyThread thread3=NewMyThread (3); MyThread Thread4=NewMyThread (4); MyThread Thread5=NewMyThread (5); MyThread Thread6=NewMyThread (6); MyThread Thread7=NewMyThread (7); MyThread Thread8=NewMyThread (8); MyThread thread9=NewMyThread (9); MyThread THREAD10=NewMyThread (10);        Thread1.start ();        Thread2.start ();        Thread3.start ();        Thread4.start ();        Thread5.start ();        Thread6.start ();        Thread7.start ();        Thread8.start ();        Thread9.start ();    Thread10.start (); }}

The results are as follows, and the order of execution is inconsistent with the order in which the start () method is called:

Java Threading Summary (i)

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.