filmov
tv
A simple multi-threading Java game with monitor pattern -- part3

Показать описание
There are 3 threads involved in the program -- main, updater and swing event dispatch thread.
There are 5 state variables for GameBoard -- dx, dy, yourtank, laserBeam, mt, they are shared among multiple threads, but none of them is published correctly.
The invariants are: dx and dy have to be updated atomically.
yourtank and laserBeam have to be published correctly, otherwise the threads referencing them can saw them in partially instantiated states.
repaint is called outside of Event dispatch thread (from updater), although the API document didn't make it clear, the repaint is thread safe by inspecting the implementation (a PaintEvent is submitted to EventQueue and executed by event dispatch thread).