import java.util.concurrent.TimeUnit; //命名匿名类-Thread class InnerThread1 { private int countDown = 5; private Inner inner; private class Inner extends Thread{ Inner(String name){ super(name); start(); } @Override public void run() { try { while (true){ System.out.println(this); if(--countDown==0) return; sleep(10); } }catch (InterruptedException e){ System.out.println("interrupted"); } } @Override public String toString() { return getName()+": "+countDown; } } public InnerThread1(String name){ inner = new Inner(name); } } //命名内部类-Runnable 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(); } @Override public void run() { try { while (true){ System.out.println(this); if(--countDown==0) return; TimeUnit.SECONDS.sleep(10); } }catch (InterruptedException e){ System.out.println("interrupted"); } } @Override public String toString() { return t.getName()+": "+countDown; } } public InnerRunnable1(String name){ inner = new Inner(name); } } //匿名内部类-Thread class InnerThread2 { private int countDown = 5; public InnerThread2(final String name){ new Thread(name){ @Override public void run() { try { while (true){ System.out.println(this); if(--countDown==0) return; sleep(10); } }catch (InterruptedException e){ System.out.println("interrupted"); } } @Override public String toString() { return getName()+": "+countDown; } }.start(); } } //匿名内部类-Runnable2 class InnerRunnable2 { private int countDown = 5; public InnerRunnable2(final String name){ new Thread(new Runnable() { @Override public void run() { try { while (true){ System.out.println(this); if(--countDown==0) return; TimeUnit.SECONDS.sleep(10); } }catch (InterruptedException e){ System.out.println("interrupted"); } } @Override public String toString() { return Thread.currentThread().getName()+": "+countDown; } }, name).start(); } } //a separate methid to run somoe code as task class ThreadMethod{ private int countDown = 5; private Thread thread; private String name; public ThreadMethod(String name) { this.name = name; } public void runTask(){ if (null==thread){ new Thread(name){ @Override public void run() { try { while (true){ System.out.println(this+""); if(--countDown==0) return; sleep(10); } }catch (InterruptedException e){ System.out.println("interrupted"); } } @Override public String toString() { return Thread.currentThread().getName()+": "+countDown; } }.start(); } } } public class Test2{ public static void main(String[] args) { new InnerThread1("InnerThread1"); new InnerRunnable1("InnerRunnable1"); new InnerThread2("InnerThread2"); new InnerRunnable2("InnerRunnable2"); new ThreadMethod("ThreadMethod"); } }
输出结果:
InnerThread1: 5 InnerThread2: 5 InnerRunnable1: 5 InnerRunnable2: 5 InnerThread1: 4 InnerThread2: 4 InnerThread2: 3 InnerThread1: 3 InnerThread1: 2 InnerThread2: 2 InnerThread1: 1 InnerThread2: 1 InnerRunnable1: 4 InnerRunnable2: 4 InnerRunnable1: 3 InnerRunnable2: 3 InnerRunnable2: 2 InnerRunnable1: 2 InnerRunnable2: 1 InnerRunnable1: 1 Process finished with exit code 0