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)