线程调度小示例:newSingleThreadScheduledExecutor用法

原创
2014/12/07 15:31
阅读数 4.1K

线程调度小示例:newSingleThreadScheduledExecutor用法

public class ScheduledExecutor {
	
	static class SegTF implements ThreadFactory{

		public Thread newThread(Runnable r) {
			Thread t = new Thread(r, "SegmentScheduledExecutorThread");
			t.setDaemon(true);
			return t;
		}
		
	}
	
	final public static ScheduledExecutorService ScheduledService = Executors.newSingleThreadScheduledExecutor(new SegTF());
	
	
	public static void submit(Runnable cmd, long periodMilliSenconds){
		ScheduledService.scheduleAtFixedRate(cmd, 10l, periodMilliSenconds, TimeUnit.MILLISECONDS);
	}
	
	
	public static void main(String[] args) throws InterruptedException {
		ScheduledExecutor.submit(new Runnable(){

			public void run() {
				System.out.println("do something");
				
			}
			
		}, 1000);
		ScheduledExecutor.submit(new Runnable(){

			public void run() {
				System.out.println("do another thing");
				
			}
			
		}, 1000);
		
		TimeUnit.SECONDS.sleep(10);
	}
	
}


展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
1 收藏
0
分享
返回顶部
顶部