JAVA不允许一个类继承自多个类,为了解决这个问题,JAVA提供了java.lang.Runnable接口,它有一个run()方法:
1 package runimpl; 2 public class Machine implements Runnable{ 3 private int a=0; 4 public void run(){ 5 for(a=0;a<50;a++){ 6 System.out.println(Thread.currentThread().getName()+":"+a); 7 try{ 8 Thread.sleep(100); 9 }catch(InterruptedException e){throw new RuntimeException(e);} 10 } 11 } 12 public static void main(String args[]){ 13 Machine machine=new Machine(); 14 Thread t1=new Thread(machine); 15 Thread t2=new Thread(machine); 16 t1.start(); 17 t2.start(); 18 } 19 }