今天偶然看到线程的问题,就随手写了一个小例子。直接上代码。
public static void main(String[] s){ ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(10, new BasicThreadFactory.Builder().namingPattern("example-schedule-%d").daemon(Boolean.TRUE).build()); for (int i = 0; i < 10; i++) { final int index = i; executorService.execute(new Runnable() { @Override public void run() { try { System.out.println(index+":"+Thread.currentThread().getName()+':'+ System.currentTimeMillis()); Thread.sleep(2000); } catch (Exception e) { e.printStackTrace(); } } }); } executorService.shutdown(); }
有不足之处,还请各位批评指正。