Inherit thread
1 Packagethread;2 //Inherit thread3 Public classTestthread {4 Public Static voidMain (string[] args) {5 //the Run () method is called by the CPU;6MyThread thread=NewMyThread ();7 Thread.Start ();8 //Main Thread9String Name=thread.currentthread (). GetName ();//get the name of the threadTen for(inti=0;i<=1000;i++){ OneSystem.out.println (name+i); A } - } - Static classMyThreadextendsthread{ the - @Override - Public voidrun () { -String Name=thread.currentthread (). GetName ();//get the name of the thread + Super. Run (); - for(inti=1;i<=10;i++){ +System.out.println (name+i); A } at } - - - } -}
Inherit runnable
1 Packagethread;2 //Inherit runnable3 Public classtestrunnable {4 Public Static voidMain (string[] args) {5 //Split Thread6Myrunnable rannable=Newmyrunnable ();7Thread thread=NewThread (rannable);8 Thread.Start ();9 //Main ThreadTenString Name=thread.currentthread (). GetName ();//get the name of the thread One for(inti=1;i<=10;i++){ ASystem.out.println (name+i); - } - } the Static classMyrunnableImplementsrunnable{ - - @Override - Public voidrun () { +String Name=thread.currentthread (). GetName ();//get the name of the thread - for(inti=1;i<=10;i++){ +System.out.println (name+i); A } at - } - - } -}
Two methods of creating threads, inheriting thread, inheriting runnable