1. Using normal inner classes to inherit the thread class
public class Threadtestone {
private int countdown = 5;
Private Inner Inner;
Private class Inner extends thread{
Inner (String name) {
super (name);
Start ();
}
public void Run () {while
(true) {
System.out.println (this);
if (--countdown = = 0) return
;
try{Sleep
(ten);
} catch (Interruptedexception e) {
throw new RuntimeException (e);
}
}} Public String toString () {return
"#" +getname () + ":" +countdown
}
}
Public Threadtestone (String name) {
inner = new Inner (name);
}
}
2. Use anonymous inner classes to construct the thread class, overriding the run () method
public class Threadtesttwo {
private int countdown = 5;
Private Thread t;
Public Threadtesttwo (String name) {
T = new Thread (name) {public
void run () {while
(true)
{ System.out.println (this);
if (--countdown = = 0) return
;
try{Sleep
(ten);
} catch (Interruptedexception e) {
throw new RuntimeException (e);
}
}} Public String toString () {return
"#" +getname () + ":" +countdown;
}
};
T.start ();
}
3. Use ordinary inner class to implement Runnable class
public class Runnabletestone {
private int countdown = 5;
Private Inner Inner;
Private class Inner implements runnable{
Thread t;
Inner (String name) {
T = new Thread (this,name);
T.start ();
}
public void Run () {while
(true) {
System.out.println (this);
if (--countdown = = 0) return
;
try{
Thread.Sleep (ten);
} catch (Interruptedexception e) {
throw new RuntimeException (e);
}
}} Public String toString () {return
"#" +thread.currentthread (). GetName () + ":" +countdown
}
}
Public Runnabletestone (String name) {
inner = new Inner (name);
}
}
4. Constructs the thread class using an anonymous inner class, parameter 1 constructs a Runnable object, and Parameter 2 indicates the thread name
public class Runnabletesttwo {
private int countdown = 5;
Private Thread t;
Public Runnabletesttwo (String name) {
T = new Thread (new Runnable () {public
void run () {while
(true)
{ System.out.println (this);
if (--countdown = = 0) return
;
try{
Thread.Sleep (ten);
} catch (Interruptedexception e) {
throw new RuntimeException (e);
}
}} Public String toString () {return
"#" +thread.currentthread (). GetName () + ":" +countdown;
}
},name);
T.start ();
}
5. Use the local inner class, that is, to construct the thread class inside the method, overriding the run () method
public class Threadmethod {private int countdown = 5;
Private Thread t;
private String name;
Public Threadmethod (String name) {this.name = name; The public void Runthread () {if (t = = null) {t = new Thread (name) {public void run (
) {while (true) {System.out.println (this);
if (--countdown = = 0) return;
try{sleep (10);
}catch (Interruptedexception e) {throw new RuntimeException (e); }} public String toString () {return "#" +getnam
E () + ":" +countdown;
}
};
T.start (); }
}
}