JAVA中共享变量实例:
1 package sharevar; 2 public class Machine extends Thread{ 3 private int a=0; //实例变量 4 public void run(){ 5 for(a=0;a<50;a++){ //使用Machine对象的实例变量a 6 System.out.println(currentThread().getName()+":"+a); 7 try{ 8 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 machine.start(); 15 machine.run(); 16 } 17 }