2016-07-01
1 inheriting the thread class creating threads
Class Name extends thread{
public void Runa () {
Related code
}
}
Packagecom.java1995;/*** Inherit thread class to create threads *@authorAdministrator **/ Public classMyThreadextendsthread{//constructors: Naming Threads PublicMyThread (String name) {Super(name); } //override the Run () method Public voidrun () { for(inti=0;i<10;i++){ Try{Thread.Sleep (1000); }Catch(interruptedexception e) {e.printstacktrace (); }//1000 msSystem.out.println ("MyThread" +i); } }}
Packagecom.java1995; Public classThreaddemo { Public Static voidMain (string[] args) {MyThread Mt=NewMyThread ("Thread 1"); MyThread mt2=NewMyThread ("Thread 2"); Mt.start ();//MultithreadingMt2.start (); //Mt.run ();//Single Thread for(inti=0;i<10;i++){ Try{Thread.Sleep (1000); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("Main thread" +i); } System.out.println ("Program execution Complete"); }}
Resources
[1] Java Easy Start Classic tutorial "full version"
Java section 54th inherits the thread class to create threads