`

定时线程执行工具 : ScheduledThreadPoolExecutor

    博客分类:
  • Java
 
阅读更多
http://2710067471.iteye.com/blog/2106223
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.Date;


public class TestScheduledThreadPoolExecutor {
	
	public static void main(String[] args) {
		ScheduledThreadPoolExecutor exec=new ScheduledThreadPoolExecutor(1);
		
		exec.scheduleAtFixedRate(new Runnable(){//每隔一段时间就触发异常
			@Override
			public void run() {
				try
				{
					throw new Exception();	
				}
				catch (Exception e)
				{
					System.out.println("异常");
				}
				
			}}, 1000, 5000, TimeUnit.MILLISECONDS);
		
		exec.scheduleAtFixedRate(new Runnable(){//每隔一段时间打印系统时间,证明两者是互不影响的
			@Override
			public void run() {
				System.out.println(new Date().getTime());
			}}, 1000, 2000, TimeUnit.MILLISECONDS);
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics