//线程数量 int threadNum = lists.size(); //创建一个线程池 ExecutorService pool = Executors.newFixedThreadPool(threadNum); for (int i = 0; i < threadNum; i++) { final List<ChannelInfo> c = lists.get(i); final int currentThreadNum = i; pool.execute(new Runnab
我们现在在Java中使用多线程通常不会直接用Thread对象了,而是会用到java.util.concurrent包下的ExecutorService类来初始化一个线程池供我们使用. 之前我一直习惯自己维护一个list保存submit的callable task所返回的Future对象. 在主线程中遍历这个list并调用Future的get()方法取到Task的返回值. public class CompletionServiceTest { static class Task implemen
翻译javadoc系列文章之:ExecutorService /** * An {@link Executor} that provides methods to manage termination and * methods that can produce a {@link Future} for tracking progress of * one or more asynchronous tasks. * * <p> An <tt>ExecutorService</
Thread和ExecutorService的区别 使用Thread,当子线程执行结束后,主线程如果没有其他执行任务,主线程会终止. /** * Created by litao on 15/10/7. */ public class ThreadTest { public static void main(String[] args) { Thread threadA=new Thread() { @Override public void run() { super.run(); Syste