知识库--Working with Multiple Actors No Using Thread Pool (139)
2017-03-12 22:07
465 查看
1 Creating Actors
2 Sending and Receiving Messages
3 Working with Multiple Actors
4 Coordinating Actors
5 Using Typed Actors
6 Typed Actors and Murmurs
7 Mixing Actors and STM
8 Using Transactors
9 Coordinating Typed Actors
10 Remote Actors
11 Limitations of the Actor-Based Model
12 epliogue
计算指定范围内的素数个数
使用线程池:计算密集型–堵塞系数接近0,线程数=cpu个数;参考之前文章
使用Actor,无需计算线程个数,纯异步,参见代码
素数的个数和判断
输出结果
结论:对比
There is no pool size to set in the Akka version of primes counting. This is a computation-intensive problem, and setting the pool size for the ExecutorService version above the number of cores made little difference. So, they’re fairly close in performance, and there is slightly less ceremony in the Akka version than in ExecutorService. This difference
becomes more prominent when we need more coordination between the threads/actors.
2 Sending and Receiving Messages
3 Working with Multiple Actors
4 Coordinating Actors
5 Using Typed Actors
6 Typed Actors and Murmurs
7 Mixing Actors and STM
8 Using Transactors
9 Coordinating Typed Actors
10 Remote Actors
11 Limitations of the Actor-Based Model
12 epliogue
计算指定范围内的素数个数
使用线程池:计算密集型–堵塞系数接近0,线程数=cpu个数;参考之前文章
使用Actor,无需计算线程个数,纯异步,参见代码
import akka.actor.UntypedActor; import akka.actor.ActorRef; import akka.actor.Actors; import akka.actor.ActorRegistry; import akka.dispatch.Future; import java.util.List; import java.util.Collections; import java.util.ArrayList; import java.util.Arrays; public class Primes extends UntypedActor { public void onReceive(final Object boundsList) { final List<Integer> bounds = (List<Integer>) boundsList; final int count = PrimeFinder.countPrimesInRange(bounds.get(0), bounds.get(1)); getContext().replySafe(count); } public static int countPrimes( final int number, final int numberOfParts) { final int chunksPerPartition = number / numberOfParts; final List<Future<?>> results = new ArrayList<Future<?>>(); for(int index = 0; index < numberOfParts; index++) { final int lower = index * chunksPerPartition + 1; final int upper = (index == numberOfParts - 1) ? number : lower + chunksPerPartition - 1; final List<Integer> bounds = Collections.unmodifiableList( Arrays.asList(lower, upper)); final ActorRef primeFinder = Actors.actorOf(Primes.class).start(); results.add(primeFinder.sendRequestReplyFuture(bounds));//Future 异步通信 } int count = 0; for(Future<?> result : results) count += (Integer)(result.await().result().get()); Actors.registry().shutdownAll(); return count; } public static void main(final String[] args) { if (args.length < 2) System.out.println("Usage: number numberOfParts"); else { final long start = System.nanoTime(); final int count = countPrimes( Integer.parseInt(args[0]), Integer.parseInt(args[1])); final long end = System.nanoTime(); System.out.println("Number of primes is " + count); System.out.println("Time taken " + (end - start)/1.0e9); } } }
素数的个数和判断
public class PrimeFinder { public static boolean isPrime(final int number) { if (number <= 1) return false; final int limit = (int) Math.sqrt(number); for(int i = 2; i <= limit; i++) if(number % i == 0) return false; return true; } public static int countPrimesInRange(final int lower, final int upper) { int count = 0; for(int index = lower; index <= upper; index++) if(isPrime(index)) count += 1; return count; } }
输出结果
Let’s go ahead and exercise the example code with a large number like 10 million and 100 parts: Number of primes is 664579 Time taken 3.890996
结论:对比
There is no pool size to set in the Akka version of primes counting. This is a computation-intensive problem, and setting the pool size for the ExecutorService version above the number of cores made little difference. So, they’re fairly close in performance, and there is slightly less ceremony in the Akka version than in ExecutorService. This difference
becomes more prominent when we need more coordination between the threads/actors.
相关文章推荐
- nsthread autoreleased with no pool in place – just leaking警告的解决方法
- 知识库--Creating Actors By Akka Using Java(139)
- Socket Server with .NET 3.5 using pooled buffers and SocketAsyncEventArgs
- Reporting Service 告警"w WARN: Thread pool pressure. Using current thread for a work item"
- Working with multiple environments
- 知识库--Concurrency+ThreadPool+Executors(79)
- Android: Reading, using and working with XML data and web services in Android
- 知识库--Sending and Receiving Messages By Akka Using Java(139)
- Using-jqGrid-s-search-toolbar-with-multiple-filter
- call to OpenGL ES API with no current context (logged once per thread)
- [MSDN]TN035: Using Multiple Resource Files and Header Files with Visual C++
- Full Multi-thread Client/Server Socket Class with ThreadPool
- Spring Boot中使用Feign调用时Hystrix提示异常:"could not be queued for execution and no fallback available."以及"Rejected command because thread-pool queueSize is at rejection threshold"
- Changes in Agile - lesson on working with multiple teams
- cocos2dx- call to OpenGL ES API with no current context(logged once per thread)
- call to OpenGL ES API with no current context (logged once per thread)
- Socket Server with .NET 3.5 using pooled buffers and SocketAsyncEventArgs(转)
- cocos2dx- call to OpenGL ES API with no current context(logged once per thread)
- log4j:WARN No appenders could be found for logger (org.quartz.simpl.SimpleThreadPool)
- cocos2dx- call to OpenGL ES API with no current context(logged once per thread)