java线程-线程池

1.Executors.newFixedThreadPool 固定线程数
2、Executors.newCachedThreadPool 按任务数创建线程数
3、newSingleThreadExecutor  单线程
4、newScheduledThreadPool 按固定时间或周期性执行一个任务
publicstaticvoid main(String[] args) {
        ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
        exec.scheduleAtFixedRate(new Runnable() {//每隔一段时间就触发异常
                      @Override
                      publicvoid run() {
                           //throw new RuntimeException();
                           System.out.println("================");
                      }
                  }, 1000, 5000, TimeUnit.MILLISECONDS);
        exec.scheduleAtFixedRate(new Runnable() {//每隔一段时间打印系统时间,证明两者是互不影响的
                      @Override
                      publicvoid run() {
                           System.out.println(System.nanoTime());
                      }
                  }, 1000, 2000, TimeUnit.MILLISECONDS);
    }
}

猜你喜欢

转载自yxpjx01.iteye.com/blog/2220027