hi all,
i haven't worked with threads much, and i have a question about how synchronzied would work in this situation. here is a stripped down example of a game class:
public class GamePanel extends JPanel implements Runnable { private Thread animator; public volatile boolean running = true; public volatile boolean isPaused = false; public GamePanel() { animator = new Thread(this); animator.start(); } @Override public void run() { while (running) { if (isPaused) { synchronized (this) { while (isPaused && running) { wait(); // try/catch omitted to keep it simple for // example } } } } } public void pauseGame() { isPaused = true; } public synchronized resumeGame() { isPaused = false; notify(); } }
Basically, I assume the user presses P and the event-dispatch thread calls the appropriate event handler which calls pauseGame(). The animator thread is always running while the game isn't paused to update/render the game.
So, let's say the user pauses the game and isPaused is called. Then, in the next iteration of the animator thread's while loop, it will enter the synchronized block and enter a wait state.
Does the object become unlocked when a thread enters the wait state? If not, how will another thread call resumeGame() since the object is already locked by the animation thread?