Created
February 3, 2014 12:52
Revisions
-
yackx created this gist
Feb 3, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,59 @@ #!/usr/bin/env groovy import static groovyx.gpars.GParsPool.withPool import groovy.transform.* import java.util.concurrent.* @TypeChecked class Philosopher { enum State { THINKING, HUNGRY, EATING } int MAX_TIME = 1 * 1000 int id State state Semaphore left, right private void doItSomeTime() { sleep(ThreadLocalRandom.current().nextLong(MAX_TIME)) } private void dine() { state = State.THINKING println this doItSomeTime() state = State.HUNGRY println this left.acquire() right.acquire() state = State.EATING doItSomeTime() println this right.release() left.release() dine() } @Override String toString() { "#${id} is $state" } } int PHILOSOPHER_COUNT = 5 List<Semaphore> forks = (1..PHILOSOPHER_COUNT).collect { new Semaphore(1) } List<Philosopher> philosophers = (1..PHILOSOPHER_COUNT).collect { i -> new Philosopher(id: i, left: forks[i - 1], right: forks[i % PHILOSOPHER_COUNT]) } withPool(PHILOSOPHER_COUNT) { philosophers.eachParallel { it.dine() } }