用一个 public static 变量!
线程启动,变量加1

解决方案 »

  1.   

    import java.io.*;
    public class autoAdd implements Runnable{
    public int i = 1;
    BufferedReader bfr;
    public static void main(String[] args){
    autoAdd aa = new autoAdd();
    try{
    new Thread(aa).start();
    }catch(Exception e){
    }
    }
    public void run() {
    try{
    bfr = new BufferedReader(new InputStreamReader(System.in));
    while(true){
    String str = bfr.readLine();
    int j = Integer.parseInt(str);
    if(j != -1){
    this.i = j;
    System.out.println("you have changed i into "+j);
    System.out.println("i is : "+i);
    }else {
    System.exit(0);
    }
    Thread.sleep(1000);
    }
    }catch(Exception e){
    }
    }
    }