线程池

2019-03-13 09:11:51 浏览数 (1)

代码语言:javascript复制
   public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(3);
        List<Future<?>> taskResults = new LinkedList<>();
        for(int i =0; i < 10; i  ){
            taskResults.add(executor.submit(new CodingTask(i)));
        }
        System.out.println("10 task finashed");

//        for (Future<?> future : taskResults){
//            future.get();
//        }

        System.out.println("all thread finished");
        executor.shutdown();
    }
代码语言:javascript复制
package handlers;

public class CodingTask implements Runnable {
    public int employId;

    public CodingTask(int employId) {
        this.employId = employId;
    }

    @Override
    public void run() {
        System.out.println("Employee"   employId   "start writing code");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
     //   System.out.println("Employee"   employId   "finish writing code");
    }
}

0 人点赞