public class SimpleThread extends Thread{
private int countDown=5;
private static int threadCount=0;
public SimpleThread(){
super(Integer.toString(++threadCount));
start();
}
public void run() {
while(true){
System.out.print(this);
if(--countDown==0)
return;
}
}
public String toString() {
return "#"+getName()+"("+countDown+")";
}
public static void main(String[] args) {
for(int i=0;i<5;i++)
new SimpleThread();
}
}
输出:
#1(5)#1(4)#1(3)#1(2)#1(1)#3(5)#3(4)#3(3)#3(2)#3(1)#5(5)#5(4)#5(3)#2(5)#2(4)#2(3)#2(2)#2(1)#5(2)#5(1)#4(5)#4(4)#4(3)#4(2)#4(1)
上述例子中,可以调用Thread构造器为Thread对象赋予具体的名称,这个名称可以通过getName()从toString()中获得。
下面可能会看到的惯用法是自管理的Runnable:
public class SelfManaged implements Runnable{
private int countDown=5;
private Thread t=new Thread(this);
public SelfManaged(){t.start();}
public void run() {
while(true){
System.out.print(this);
if(--countDown==0)
return;
}
}
public String toString() {
return Thread.currentThread().getName()+"("+countDown+")";
}
public static void main(String[] args) {
for(int i=0;i<5;i++)
new SelfManaged();
}
}
有时通过使用内部类来将线程代码隐藏在类中将会很有用,就像:
public class InnerThread1 {
private int countDown=5;
private Inner inner;
private class Inner extends Thread{
Inner(String name){
super(name);
start();
}
public void run(){
try{
while(true){
System.out.println(this);
if(--countDown==0)return;
sleep(10);
}
}catch(InterruptedException e){
System.out.println("interrupted");
}
}
public String toString(){
return getName()+":"+countDown;
}
}
public InnerThread1(String name){
inner=new Inner(name);
}
}
public class InnerThread2 {
private int countDown=5;
private Thread t;
public InnerThread2(String name){
t=new Thread(name){
public void run(){
try{
while(true){
System.out.println(this);
if(--countDown==0)return;
sleep(10);
}
}catch(InterruptedException e){
System.out.println("sleep() interrupted");
}
}
public String toString(){
return getName()+":"+countDown;
}
};
t.start();
}
}
public class InnerRunnable1 {
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(){
try{
while(true){
System.out.print(this);
if(--countDown==0)return;
TimeUnit.MILLISECONDS.sleep(10);
}
}catch(InterruptedException e){
System.out.println("sleep() interrupted");
}
}
public String toString(){
return t.getName()+":"+countDown;
}
}
public InnerRunnable1(String name){
inner=new Inner(name);
}
}
public class InnerRunnable2 {
private int countDown=5;
private Thread t;
public InnerRunnable2(String name){
t=new Thread(new Runnable(){
public void run(){
try{
while(true){
System.out.println(this);
if(--countDown==0)return;
TimeUnit.MILLISECONDS.sleep(10);
}
}catch(InterruptedException e){
System.out.println("sleep()interrupted");
}
}
public String toString(){
return Thread.currentThread().getName()+":"+countDown;
}
},name);
t.start();
}
}
public class ThreadMethod {
private int countDown=5;
private Thread t;
private String name;
public ThreadMethod(String name){this.name=name;}
public void runTask(){
if(t==null){
t=new Thread(name){
public void run(){
try{
while(true){
System.out.println(this);
if(--countDown==0)return;
sleep(10);
}
}catch(InterruptedException e){
System.out.println("sleep() interrupted");
}
}
public String toString(){
return getName()+":"+countDown;
}
};
t.start();
}
}
}
InnerThread1创建了一个扩展自Thread的匿名内部类。InnerThread2展示了可替换的方式:在构造器中创建一个匿名的Thread子类,并且将其向上转型为Thread引用t,如果类中的其他方法需要访问t那它们可以通过Thread接口来实现。
三四个类重复了前两个类,但它们使用的是Runnable接口而不是Thread类。
ThreadMethod类展示了在方法内部如何创建线程,当准备好运行线程时,就可以调用这个方法,而在线程开始之后,该方法将返回,如果该线程只执行辅助操作,而不是重要操作,那么这是一个更加有用且适用的方式。