线程调度小示例:newSingleThreadScheduledExecutor用法

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

线程调度小示例: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);
	}
	
}


转载于:https://my.oschina.net/cloudcoder/blog/353410

你可能感兴趣的:(线程调度小示例:newSingleThreadScheduledExecutor用法)