Actor模式是一种并发模型,区别于共享内存模型,其不共享任何东西。多线程之间是通过消息传递的方式进行协作(传递数据),而这些线程就是Actor。共享内存更适合单机多核的并发编程,随着多核并发(分布式)系统普及,共享内存模型已然不再适用于并发编程。
下面通过示例介绍下,使用Actor并发模型解决下面的问题。
示例:对整形数组进行排序,获取其中的最大值与最小值。
package com.zte.sunquan.demo.actor.sortint;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
/**
* Created by sunquan on 2017/6/17.
*/
public class IntArraySort {
private static int[] inputs = {
2, 3, 2,
1, 12, 32,
12, 44, 56,
12, 42, 121,
555, 12, 1000,
29, -1
};
public static void main(String[] args) {
//创建ActorSystem
ActorSystem actorSystem = ActorSystem.create("int-arry-sort");
//创建actor
ActorRef mySortActorRef = actorSystem.actorOf(Props.create(MySortActor.class), "MySortActor");
//mysortActorRef被传递(告知)一个数组
mySortActorRef.tell(inputs, ActorRef.noSender());
}
}
package com.zte.sunquan.demo.actor.sortint;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import com.zte.sunquan.demo.Pair;
import java.util.Arrays;
/**
* Created by sunquan on 2017/6/17.
*/
public class MySortActor extends UntypedActor {
public final static int divCount = 3;
public int dataSize = 0;
public int returnTimes = 0;
public Pair resutPair = new Pair(Integer.MAX_VALUE, Integer.MIN_VALUE);
@Override
public void onReceive(Object o) throws Exception {
if (o instanceof int[]) {
//拆分数组,分别排序
int[] inputs = (int[]) o;
int i = 0;
dataSize = inputs.length;
for (i = 0; i < inputs.length / divCount; i++) {
int[] data = Arrays.copyOfRange(inputs, i * divCount, (i + 1) * divCount);
ActorRef arraySortActorRef = getContext().actorOf(Props.create(ArraySortActor.class));
arraySortActorRef.tell(data, this.self());
}
if (i * divCount < inputs.length) {
ActorRef arraySortActorRef = getContext().actorOf(Props.create(ArraySortActor.class));
arraySortActorRef.tell(Arrays.copyOfRange(inputs, i * divCount, inputs.length),
this.self());
}
} else if (o instanceof Pair) {
Pair pair = (Pair) o;
System.out.println(pair.getMinValue() + "," + pair.getMaxValue());
if (resutPair.getMinValue().compareTo(pair.getMinValue()) > 0) {
resutPair.setMinValue(pair.getMinValue());
}
if (resutPair.getMaxValue().compareTo(pair.getMaxValue()) < 0) {
resutPair.setMaxValue(pair.getMaxValue());
}
returnTimes++;
int times = dataSize % divCount == 0 ? dataSize / divCount : dataSize / divCount + 1;
if (times == returnTimes) {
System.out.println("----------------------");
System.out.println(resutPair.getMinValue() + "," + resutPair.getMaxValue());
getContext().system().shutdown();
}
}
}
}
package com.zte.sunquan.demo.actor.sortint;
import akka.actor.ActorRef;
import akka.actor.UntypedActor;
import com.zte.sunquan.demo.Pair;
import java.util.Arrays;
/**
* Created by sunquan on 2017/6/17.
*/
public class ArraySortActor extends UntypedActor {
@Override
public void onReceive(Object o) throws Exception {
if (o instanceof int[]) {
int[] inputs = (int[]) o;
//排序
Arrays.sort(inputs);
getSender().tell(new Pair<Integer>(inputs[0], inputs[inputs.length - 1]), ActorRef.noSender());
}
}
}
打印:
2,3
1,32
12,56
-1,29
12,1000
12,121
----------------------
-1,1000
总结:
以上只作了一次拆分,代码还可以优化成多次拆分。
示例:获取【2~10^8】内的素数个数