java timeTask

873 ワード

          ,        。google  :
public class TimerTest {
	
	public static void main(String[] args) {
		Timer timer = new Timer();
		
		timer.schedule(new MyTask(), 1000, 2000);//  1       ,    2 ,      Data  ,                 .
		
		while (true) {//            ,             
			try {
				int ch = System.in.read();
				if (ch - 'c' == 0) {
					timer.cancel();//           
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	static class MyTask extends TimerTask {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			System.out.println("________");
		}
	}
}