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
- Presets:
- sol0: Mutex, only one philo can eat at a time.
- sol1: Sem for each fork, deadlocks are possible (written in French)
- sol2: Sem for each philo, philosophers wake one another to eat
- sol3: The last philo is left-handed
- sol4: Only
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()
orpenser()
: Lasts for 5-10 seconds, refills philosopher's "health". -
eat()
ormanger()
: Lasts for 5-10 seconds -
takeFork(i)
orprendreFourchette(i)
: Actually take the fork! -
putFork(i)
orposerFourchette(i)
: Actually put the fork down! -
Sem(n)
: Same asnew Semaphore(n, true)
-
p(sem)
: Same assem.acquire()
-
v(sem)
: Same assem.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.
- Alternative scripting engine: Mozilla's Rheno
- Oracle's Java Scripting Programmer's Guide
- Andrew S. Tanenbaum - Modern Operating Systems 4th ed.
-
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