就是利用匿名内部类实现,补充下,做个笔记吧

 





 


  1. import java.util.Date;  
  2.   
  3. public class ThreadInnerClass {  
  4.   
  5.     /** 
  6.      * @主要看使用内部类怎样实现一个线程 
  7.      */  
  8.     public static void main(String[] args) {  
  9.         // TODO Auto-generated method stub  
  10.         new Thread(new ThreadInner()).start();//相当于new Thread(实现Runnable接口)  
  11.           
  12. //      new Thread(new ThreadInner(),"线程1").start();//同上,(取个名字)相当于new Thread(实现Runnable接口),ThreadInner对象操作线程  
  13.           
  14. //      new ThreadInner().run();//线程也会运行,不过是这个main在操作线程。  
  15.           
  16.         /* 
  17.         new Thread(new Runnable(){//相当于是实现runnable接口 
  18.             @Override 
  19.             public void run(){ 
  20.                  
  21.                 while(true) 
  22.                 { 
  23.                     try { 
  24.                         Thread.sleep(1000); 
  25.                     } catch (InterruptedException e) { 
  26.                         // TODO Auto-generated catch block 
  27.                         e.printStackTrace(); 
  28.                     } 
  29.                      
  30.                     System.out.println("1"); 
  31.                      
  32.                 } 
  33.                  
  34.             } 
  35.              
  36.         }).start(); 
  37.         */  
  38.           
  39.           
  40.         /* 
  41.         new Thread(){//直接重写run方法 
  42.             @Override 
  43.             public void run(){ 
  44.                 System.out.println("2"); 
  45.             } 
  46.              
  47.         }.start(); 
  48.         */  
  49.           
  50.         /* 
  51.         new ThreadInner2(){//相当于继承Thread,重写run方法。和上面直接重写run方法同理 
  52.             @Override 
  53.             public void run(){ 
  54.                 System.out.println("3"); 
  55.             } 
  56.              
  57.         }.start(); 
  58.         */  
  59.           
  60.     }  
  61.   
  62. }  
  63.   
  64. class ThreadInner implements Runnable{  
  65.       
  66.     @Override  
  67.     public void run(){  
  68.           
  69.         while(true)  
  70.         {  
  71.             try {  
  72.                 Thread.sleep(1000);  
  73.             } catch (InterruptedException e) {  
  74.                 // TODO Auto-generated catch block  
  75.                 e.printStackTrace();  
  76.             }  
  77.               
  78.             System.out.println(Thread.currentThread().getName()+"  " + new Date());  
  79.         }  
  80.     }  
  81. }  
  82.   
  83.   
  84. class ThreadInner2 extends Thread{  
  85.       
  86. }