编写一个定时器(通过实现Runnable 接口)

package mythread;

public class Timer implements Runnable {
	private Thread t; //定义一个线程
	@Override
	public void run() {
		try {
			for (int i = 10; i > 0; i--) {
				System.out.println( i);
				// 让线程睡眠一会
				Thread.sleep(1000);//每隔1s执行一次循环
			}
		} catch (InterruptedException e) {
			System.out.println("Thread  interrupted.");
		}

	}

	public static void main(String[] args) {
		Timer timer = new Timer();
		timer.run();
	}
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务