知识库--Working with Multiple Actors No Using Thread Pool (139)

    xiaoxiao2021-03-25  84

    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,无需计算线程个数,纯异步,参见代码

    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.

    转载请注明原文地址: https://ju.6miu.com/read-37300.html

    最新回复(0)