java定时器timer和timerTask

package com.asiainfo.np.timer;

import java.util.Timer;
import java.util.TimerTask;
public class Tst2 extends TimerTask{
	@Override
	public void run() {//以每个run执行的时间为准,如果未完成,下一个任务就不能执行!
//		Long g = System.currentTimeMillis();
//		System.out.println("yes1"+g);
//		try {
//			Thread.sleep(998L);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		System.out.println("yes2"+g);
		
		Runnable r = new Runnable(){
			public void run() {
				try {
					Long g = System.currentTimeMillis();
					System.out.println("yes1"+g);
					Thread.sleep(6988L);
					System.out.println("yes2"+g);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
		};
		Thread t = new Thread(r);
		t.start();
	}
	
	public static void main(String[] args) throws InterruptedException {
		Timer t = new Timer();
		t.schedule(new Tst2(), 0L,1921L);
		Thread.sleep(4321L);
		t.cancel();
	}
}
 

你可能感兴趣的:(java,thread)