只接写要实现什么东西啦
int statue=1;
int time=300;
现在要写一个timer计时器,如果时间超过300S,就把statue变为0

解决方案 »

  1.   

    用线程嘛        new Thread() {
                public void run() {
                    try {
                        sleep(time * 1000);
                    } catch (InterruptedException e) {
                    }
                    status = 0;
                }
            }.start();
      

  2.   

    用Timer实现如下:
    1、定义作业任务继承TimerTask ,重写run方法:
    import java.util.TimerTask;
    public class SimpleTimerTask extends TimerTask {
        @Override
        public void run() {
    Status.status = 0;
    System.out.println(Status.status);
        }
    }
    2、使用Timer执行作业任务:
    import java.util.Timer;
    public class TimerRun {
        public static void main(String[] args) {
    System.out.println(Status.status);
    Timer start = new Timer();
    start.schedule(new SimpleTimerTask(), 300*1000);//这里是毫秒表示,300S要乘以1000
        }
    }
    public class Status {
        public static int status = 1;
    }