Search in sources :

Example 1 with ActorThread

use of actor4j.core.ActorThread in project actor4j-core by relvaner.

the class ActorBalancingOnRuntime method registerCell.

public void registerCell(Map<UUID, Long> cellsMap, List<Long> threadsList, Map<Long, ActorThread> threadsMap, Map<UUID, Long> groupsMap, Map<UUID, Integer> groupsDistributedMap, ActorCell cell) {
    try {
        lock.lock();
        Actor actor = cell.getActor();
        if (actor instanceof ActorGroupMember) {
            Long threadId = groupsMap.get(((ActorGroupMember) actor).getGroupId());
            if (threadId == null) {
                threadId = pollThreadId(threadsMap);
                groupsMap.put(((ActorGroupMember) actor).getGroupId(), threadId);
            }
            cellsMap.put(cell.getId(), threadId);
        } else if (actor instanceof ActorDistributedGroupMember) {
            Integer threadIndex = groupsDistributedMap.get(((ActorDistributedGroupMember) actor).getGroupId());
            Long id = null;
            if (threadIndex == null) {
                id = threadsList.get(pollThreadIndex.get());
                groupsDistributedMap.put(((ActorDistributedGroupMember) actor).getGroupId(), pollThreadIndex.get());
            } else {
                threadIndex++;
                if (threadIndex == threadsMap.size())
                    threadIndex = 0;
                groupsDistributedMap.put(((ActorDistributedGroupMember) actor).getGroupId(), threadIndex);
                id = threadsList.get(threadIndex);
            }
            cellsMap.put(cell.getId(), id);
            pollThreadIndex.updateAndGet((index) -> index == threadsList.size() - 1 ? 0 : index + 1);
        } else
            cellsMap.put(cell.getId(), pollThreadId(threadsMap));
    } finally {
        lock.unlock();
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Actor(actor4j.core.actors.Actor) ActorGroupMember(actor4j.core.actors.ActorGroupMember) ActorDistributedGroupMember(actor4j.core.actors.ActorDistributedGroupMember) UUID(java.util.UUID) ActorCell(actor4j.core.ActorCell) List(java.util.List) Lock(java.util.concurrent.locks.Lock) ActorThread(actor4j.core.ActorThread) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ActorGroupMember(actor4j.core.actors.ActorGroupMember) Actor(actor4j.core.actors.Actor) ActorDistributedGroupMember(actor4j.core.actors.ActorDistributedGroupMember)

Aggregations

ActorCell (actor4j.core.ActorCell)1 ActorThread (actor4j.core.ActorThread)1 Actor (actor4j.core.actors.Actor)1 ActorDistributedGroupMember (actor4j.core.actors.ActorDistributedGroupMember)1 ActorGroupMember (actor4j.core.actors.ActorGroupMember)1 List (java.util.List)1 Map (java.util.Map)1 Queue (java.util.Queue)1 UUID (java.util.UUID)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1