Where to use multithreading in Java Game Dev (Swing)

Viewed 31

I am developing a game using Java Swing and I've been wondering how can I implement multithreading into it. I thought I could move some parts of the game logic to new threads (like weather implementation/lighting effect or some complex in-game events that are running in the background independently of the main thread). I would like to hear possible suggestions and common practices on how to do it as well as possible elements of the game that are best to be implemented in individual threads. Currently it uses a single thread. Here is the core of the game (game loop, where things are updated (game logic) and rendered) :

@Override
public void run() {

    double drawInterval = 1000000000.0/FPS;
    double delta = 0;
    long lastTime = System.nanoTime();
    long currentTime;

    while(gameThread != null) {

        currentTime = System.nanoTime();

        delta += (currentTime - lastTime) / drawInterval;

        lastTime = currentTime;

        if(delta >= 1) {
            // 1 UPDATE: update information such as character position
            update();

            // 2 DRAW: draw the screen with the updated information
            repaint();

            delta--;
        }
    }
}


public void startGameThread() {
    gameThread = new Thread(this);
    gameThread.start();
}

(And then it is called in the main method...)

0 Answers
Related