Dinning philosophers problem visualization in Java #JavaFX #JavaScriptingAPI #Nashorn
(This is a rough work-in-progress--see the TODO file.)
After Trois Threads (useless vizualization) and Traffic Sim (web simulation), I think it's time for a more serious simulation in Java.
Write the philosopher(i)
(or philosophe(i)
) procedure to solve the dining philosophers problem using semaphores.
From the command line (on Linux!):
java -jar philo.jar PRESET
N - 1
philos can attempt to acquire forks at any given time* These are read from the presets
folder in the current working directory.
N
is the total number of philosophers
think()
or penser()
: Lasts for 5-10 seconds, refills philosopher's "health".
eat()
or manger()
: Lasts for 5-10 seconds
takeFork(i)
or prendreFourchette(i)
: Actually take the fork!
putFork(i)
or poserFourchette(i)
: Actually put the fork down!
Sem(n)
: Same as new Semaphore(n, true)
p(sem)
: Same as sem.acquire()
v(sem)
: Same as sem.release()
...
Nashorn to execute user code
JavaScript's setTimeout
/setInterval
counterpart in Java: Timer and TimerTask.
Visualizing using SVG by updating it through the Document
interface via JavaFX's WebEngine.
The "animation" is partially inspired by Simon Ingeson's Hungry Thinkers.
The logo is composed from
By @djalilhebal and @wanisramdani, under CC BY 3.0