Last active
December 14, 2015 14:58
-
-
Save stepancheg/5104310 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// реализация пользовательского актёра | |
class CounterActor implement Runnable { | |
// ... | |
// очередь входящих сообщений | |
// очередей может быть несколько | |
private final LockFreeStackWithSize<BigInteger> workQueue = | |
new LockFreeStackWithSize<BigInteger>(); | |
// этот метод вызывает клиент актёра, чтобы послать актёру задание | |
public void addWork(BigInteger item) { | |
// добавляем задание в очередь | |
workQueue.add(item); | |
// ... и сразу после этого говорим шедулеру запустить актёра | |
thread.schedule(); | |
} | |
// реализация кода актёра, выполняется всегда в один поток | |
public void run() { | |
// достаём все задания, что есть | |
List<BigInteger> items = workQueue.dequeueAll(); | |
for (BigInteger item : items) { | |
// что-то полезное | |
} | |
// проверяем другие очереди и флаги, есть есть | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// код, который создаёт актёра | |
// используем общий пул потоков, чтобы не тратить CPU больше, чем процессоров в системе | |
Executor executor = // ... | |
CounterActor actor = new CounterActor(executor); | |
// актёр готов. Теперь чтобы посылать актёру сообщения, нужно вызывать метод addWork | |
// метод -- потокобезопасный | |
actor.addWork(BigInteger.valueOf(1719)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment