最近,项目中需要给手机发短信的功能,我们用的是第三方短信服务商。功能具体为给批量用户发短信,我用的是ForkJoinPool,可以说ForkJoinPool特别适合这个场景。在网上看见很多人用的是Executor Framework的ExecutorService。 现在问题来了,都可以实现这个功能,哪个更好呢,他们有什么区别呢。下面是我在外国网站拜读的,自认说的很透彻,现翻译过来分享给大家
原文: Java 5 added Executor Framework to provide out-of-box thread pool to Java programmers and Java 7 added ForkJoinPool an implementation of ExecutorService which specifically designed to execute ForkJoinTask. The Executor Framework provides several classes e.g. Executor, ExecutorService, and Executors for execution and creating thread pools. It also provides several built-in, ready to use thread pools like a pool of fixed threads, cached thread pool which can expand itself, spawn new threads if required due to heavy load.
译文: java5为开发者增加了Executor框架用来提供即插即用的线程池,Java7增加了ForkJoinPool组件,它是ExecutorService一个实现类,专门被设计用来执行ForkJoinTask任务的。Executor框架提供了一些类,如Executor,ExecutorService,Executors用于执行和创建线程池,他也提供了一些内建的、有用的线程池,像固定大小、带有缓存功能的线程池,如果需要这些线程池能扩展、产生新的线程
原文: The Job of that thread pool is to accept the task and execute if there is free worker thread available, but ForJoinPool is a special kind of thread pool. They use work stealing pattern. All threads in a fork join pool attempt to find and execute tasks submitted to the pool and/or created by other active tasks.
译文: 线程池的工作是为了接收任务然后执行这个任务,前提是池里有空闲的、有效的工作线程。而ForJoinPool组件是一个特殊类型的线程池。他使用了工作窃取模式(work stealing pattern),在fork join池中的所有线程都会试图发现和执行被提交给这个池子的或被其他任务创建的那些任务
原文: This enables efficient processing when most tasks spawn other subtasks (as do most ForkJoinTasks e.g. recursive actions), as well as when many small tasks are submitted to the pool from external clients.
译文: 当许多任务创建了子任务时工作窃取模式能激活高效的处理过程。同样,当许多小任务从外部客户端被提交到这个池子时也会是高效的处理过程
原文: In short, the main difference between Executor framework and ForkJoinPool is that former provides a general purpose thread pool, while later provides a special implementation which uses work stealing pattern for efficient processing of ForkJoinTask. Let's see a couple of more differences to answer this question better.
译文: 简而言之,Executor框架和ForkJoinPool组件主要的不同为:前者提供了一般目的的线程池,而后者提供了特殊的实现,它使用工作窃取模式为了ForkJoinTask任务能高效处理。让我们看看更多的不同点去更好的回答这个问题
ForkJoinPool组件和Executor框架对比
原文: When you compare ForkJoinPool with Executor Framework, you either compare the ForkJoinPool with ThreadPoolExecutor class or directly with ExecutorService interface. In the first paragraph, I have compared the ForkJoinPool with ExecutorService, now let's see some difference between ForkJoinPool and ThreadPoolExecutor class.
译文: 当你比较ForkJoinPool组件和Executor框架时,你也可以比较ForkJoinPool组件和ThreadPoolExecutor类或者直接和ExecutorService接口比较 在前面的部分,我已经比较了ForkJoinPool和ExecutorService,现在让我们看看ForkJoinPool和ThreadPoolExecutor类的不同
原文:
- The main difference between ForkJoinPool and ThreadPoolExecutor is that ForkJoinPool is designed to accept and execute ForkJoinTask, which is a lightweight version of FutureTask, while ThreadPoolExecutor is designed to provide a normal thread pool which executes each submitted task using one of possibly several pooled threads.
译文:
- ForkJoinPool和ThreadPoolExecutor主要的不同为:ForkJoinPool被用来接收和执行ForkJoinTask任务的,ForkJoinTask是FutureTask轻量级的版本。而ThreadPoolExecutor被设计用来提供给普通的线程池的,这个普通的线程池执行被提交到这个池子的每个任务的
原文: 2) Another key difference between ThreadPoolExecutor and ForkJoinPool class is that ForkJoinPool uses work stealing pattern, which means one thread can also execute a pending task from another thread. This improves efficient in case of ForkJoinTask as most of ForkJoinTask algorithm spawn new tasks. You can further read Java Concurrency in Practice by Brian Goetz to learn more about work stealing pattern and other parallel computing patterns.
译文: 2) ForkJoinPool和ThreadPoolExecutor另一个的不同为:ForkJoinPool使用了工作窃取模式,意味着一个线程能执行本来要另一个线程执行的任务。在ForkJoinTask产生子ForkJoinTask的情况下这个模式是高效率的。你能继续阅读java并发编程这本书,通过它可以学到更多关于工作窃取模式和其他的并发计算模式
原文: 3) Some of the common ThreadPoolExecutor provided by JDK API is SingleThreadExecutor, which is a thread pool of just one background thread, a cached thread pool provided by Executors.newCachedThreadPool, which is a pool of unbounded threads, where the pool can spawn new threads if needed and reclaim old threads when not needed.
译文: 被JDK API提供的一些ThreadPoolExecutor是SingleThreadExecutor,它是一个有后台线程的线程池、一个被Executors.newCachedThreadPool提供的带有缓存的线程池,Executors.newCachedThreadPool是一个没有绑定线程的池。如果需要这个池子能生成新线程,当不需要的时候可以剔除旧的线程
原文: The third type of common use of ThreadPoolExecutor is fixed thread pool, where the pool has a fixed number of threads in its lifetime, it's also an example of a bounded thread pool. That's all about the difference between ForkJoinPool and Executor Framework in Java. You should use ForkJoinPool if you are using that framework and submit ForkJoinTask, otherwise just use a ThreadPoolExecutor instance, provided by various factory methods of Executors class e.g. Executors.newSingleThreadPoolExecutor(), Executors.newCachedThreadPoolExecutor() and Executors.newFixedThreadPoolExecutor() class.
译文: ThreadPoolExecutor第三种类型的使用是固定的线程池,这个池子拥有固定数量的线程,他也是绑定线程池的一个例子。这些就是Java中executor-framework和ForkJoinPool的不同点。如果你正在提交ForkJoinTask任务,你应该使用ForkJoinPool组件,否则应该使用ThreadPoolExecutor,被提供了各种工厂方法类,如Executors.newSingleThreadPoolExecutor(), Executors.newCachedThreadPoolExecutor() and Executors.newFixedThreadPoolExecutor() class.
原文: 3) Some of the common ThreadPoolExecutor provided by JDK API is SingleThreadExecutor, which is a thread pool of just one background thread, a cached thread pool provided by Executors.newCachedThreadPool, which is a pool of unbounded threads, where the pool can spawn new threads if needed and reclaim old threads when not needed.
译文: 被JDK API提供的一些ThreadPoolExecutor是SingleThreadExecutor,它是一个有后台线程的线程池、一个被Executors.newCachedThreadPool提供的带有缓存的线程池,Executors.newCachedThreadPool是一个没有绑定线程的池。如果需要这个池子能生成新线程,当不需要的时候可以剔除旧的线程
原文: The third type of common use of ThreadPoolExecutor is fixed thread pool, where the pool has a fixed number of threads in its lifetime, it's also an example of a bounded thread pool. That's all about the difference between ForkJoinPool and Executor Framework in Java. You should use ForkJoinPool if you are using that framework and submit ForkJoinTask, otherwise just use a ThreadPoolExecutor instance, provided by various factory methods of Executors class e.g. Executors.newSingleThreadPoolExecutor(), Executors.newCachedThreadPoolExecutor() and Executors.newFixedThreadPoolExecutor() class.
译文: ThreadPoolExecutor第三种类型的使用是固定的线程池,这个池子拥有固定数量的线程,他也是绑定线程池的一个例子。这些就是Java中executor-framework和ForkJoinPool的不同点。如果你正在提交ForkJoinTask任务,你应该使用ForkJoinPool组件,否则应该使用ThreadPoolExecutor,被提供了各种工厂方法类,如Executors.newSingleThreadPoolExecutor(), Executors.newCachedThreadPoolExecutor() and Executors.newFixedThreadPoolExecutor() class.