Search code examples
javamethodssynchronizedrunnable

What exactly does a synchronized method do?


I'm creating a game using Swing. I made start() and stop() synchronized because I was told it was better. What does synchronized do and what is the advantage in using it?

My code:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JFrame;

public class SpritePractice extends Canvas implements Runnable{

private JFrame frame;
private final static int WIDTH = 200, HEIGHT = 200;
private final static int SCALE = 2;
private final static Dimension dimens= new Dimension(WIDTH*SCALE, HEIGHT*SCALE);
private BufferedImage image;
private Graphics g;
private long nanoSecond = 1000000000;
private double tick = nanoSecond/60;
private boolean running = false;
private int pixelsFromImage[];
private int pixel[][];
private static DateFormat dateFormat = new SimpleDateFormat("[" + "yyyy/MM/dd HH:mm:ss"
        +"]");
private static DateFormat dateFormat2 = new SimpleDateFormat("[" + "HH:mm:ss" + "]");

public SpritePractice()
{
    frame = new JFrame("Bomberman");
    frame.setSize(dimens);
    frame.setMinimumSize(dimens);
    frame.setMaximumSize(dimens);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(this);
    frame.pack();
    frame.setVisible(true);
    init();
}
public void init()
{
    long startTime = System.nanoTime();
    Calendar cal = Calendar.getInstance();
    System.out.println("START: " + dateFormat.format(cal.getTime()));
    start();
}

public void run() 
{
    long now = System.nanoTime();
    long lastTick = System.nanoTime();
    long lastSecond = System.nanoTime();
    int frames = 0;

    while(running)
    {
        now = System.nanoTime();
        Calendar cal = Calendar.getInstance();

        if(now-lastTick >= tick)
        {
            lastTick = now;
            tick();
            render();
            frames++;
        }   
        if(now-lastSecond >= nanoSecond)
        {
            lastSecond = now;
            System.out.println(dateFormat2.format(cal.getTime()) + "FPS: " + frames);
            frames = 0;
        }
    }
}
public void tick()
{
    //updates values
}
public void render()
{
    BufferStrategy bs = getBufferStrategy();
    if(bs==null)
    {
        createBufferStrategy(2);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    g.fillRect(0, 0, WIDTH*2, HEIGHT*2);
    g.dispose();
    bs.show();
    //renders graphics
}
public synchronized void start()
{
    running = true;
    run();
}
public synchronized void stop()
{
    running = false;
}
public static void main(String[] args)
{
    new SpritePractice();
}

}


Solution

  • The thread that enters a synchronized method obtains a lock on the entire object who owns the method.

    In your particular case, you can be sure that there won't be two concurrent threads that execute start() and stop() simultaneously.

    Read here more about synchronized methods.

    e.g. if one thread enters the start() method, it will complete its execution before another thread could enter the stop() method. Without synchronizing the 2 methods, the following succession is possible:

    -- thread 1 enters start()
    -- thread 1 sets boolean field to true
    -- thread 2 enters stop()
    -- thread 2 sets boolean field to false
    -- thread 1 executes run() method

    which you definitely don't want.