前面文章用直接使用Thread类的start方法来新启动一个线程,看起来貌似没有什么问题,问题就在于线程使用场景的需求上,如果希望每个一段时间自动运行一次这个线程,如果想使用线程池,这个Start方法就完全不够了。下面我们来看看Java并发包中的Executor接口吧
Java代码 public interface java.util.concurrent.Executor { public void execute(java.lang.Runnable arg0); }实际上是把调度线程的功能抽象出来,java 5.0内建了不少Executor的实现,当然你自己也可以去实现这个接口:在当前线程中启动一个功能
Java代码 import java.util.concurrent.Executor; public class myExecutor implements Executor { @Override public void execute(Runnable arg0) { arg0.run(); } }在新线程中其中一个功能
Java代码 import java.util.concurrent.Executor; public class myExecutor implements Executor { @Override public void execute(Runnable arg0) { new Thread(arg0).start(); } }以上两个例子让您明白,你可以实现你需要的线程启动模型!对于内建的Executor,其实也基本够用了:单线程:顾名思义就是一个线程,和Thread也没有什么太大区别
Java代码 Executor oneThread = Executors.newSingleThreadExecutor();固定线程数的池:固定数量的线程,但是加入了池的能力,在一个线程完成任务后不会释放其在内存中位置,而是“缓存”起来,一旦一个新的任务到来就会“重用”这个线程的空间。但是其数量是受到限制的,一旦超过,也不会出现什么异常,只是等待时间会变得长一些,就是感觉慢一些罢了。
Java代码 Executor fixedPool = Executors.newFixedThreadPool(10);无限数量的线程池:很好的线程池,除了和FixedThreadPool有着相同的能力,但是线程数却使无限的,也就是说每加入一个线程就会池化,但是无限的对内存的有限,显然是悖论,所以加得越多,重用机会也减少了,内存占有越来越大,不是很好的方式了。最好是根据需要来使用FixedThreadPool和CachedThreadPool!
Java代码 Executor unboundedPool = Executors.newCachedThreadPool();另外你还看到我们在使用这些内建的Executor时,发现都是Executors这个类负责的,不要大惊小怪,不过就是一个工厂类,提供一些静态工厂方法来便利地帮助你创建这些Executor实例而已。实际上,Executor接口只是这些内建的Executor的根,它们其实是直接实现了ExecutorService接口,而ExectorService和Executor接口的关系是:
Java代码 public interface java.util.concurrent.ExecutorService extends java.util.concurrent.Executor { public void shutdown(); public java.util.List shutdownNow(); public boolean isShutdown(); public boolean isTerminated(); public boolean awaitTermination(long arg0, java.util.concurrent.TimeUnit arg1) throws java.lang.InterruptedException; public java.util.concurrent.Future submit(java.util.concurrent.Callable arg0); public java.util.concurrent.Future submit(java.lang.Runnable arg0, java.lang.Object arg1); public java.util.concurrent.Future submit(java.lang.Runnable arg0); public java.util.List invokeAll(java.util.Collection arg0) throws java.lang.InterruptedException; public java.util.List invokeAll(java.util.Collection arg0, long arg1, java.util.concurrent.TimeUnit arg2) throws java.lang.InterruptedException; public java.lang.Object invokeAny(java.util.Collection arg0) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException; public java.lang.Object invokeAny(java.util.Collection arg0, long arg1, java.util.concurrent.TimeUnit arg2) throws java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException;一看就看明白了,这个接口头拓展于Executor,并且内建的Executor大多从ExecutorService实现的,在Executor的基础上,第一加入了生命周期控制方法,如shutdown,第二加入了对Future的支持(在上一讲已经有Future的例子了,下面还有一个),那么如果打个比方,Thread类自行车发条,Executor是摩托车发动机,ExecutorService就是一个跑车发动机.
Java代码 import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; public class FutureTaskTest { private static AtomicInteger Count = new AtomicInteger(0) @SuppressWarnings("unchecked") public static void main(String args[]) { ExecutorService es = Executors.newFixedThreadPool(10); List<Future<Integer>> tasks = new ArrayList<Future<Integer>>(); for (int i = 0; i <= 10; i++) { FutureTask<Integer> futureTask = new FutureTask<Integer>( new Callable() { @SuppressWarnings("static-access") public Integer call() throws Exception { Thread.currentThread().sleep( (new Random()).nextInt(1000)); return Count.getAndIncrement(); } }); tasks.add(futureTask); es.submit(futureTask); } Integer result = new Integer(0); try { for (Future<Integer> task : tasks) { result += (Integer) task.get(); } es.shutdown(); System.out.println(result); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }那么除了上面提高的内建类,还有什么吗?ScheduledExecutorService 一看名字就猜出来了,它可以Schedule去执行线程!也就是指定一个时间、间隔去执行线程中的任务.
以上都是用来调度线程每隔一个时间间隔执行的方法,不仅有Runnable的,还有Callable的。
经常能用到的东西就这些,下一讲是有关线程锁的话题.
转载于:https://www.cnblogs.com/isoftware/p/3794359.html
相关资源:数据结构—成绩单生成器