final Timer timer = new Timer(); timer.schedule(new TimerTask(){ void run(){ // 执行程序 timer.cancel(); } }, 5000);