Saturday, May 4, 2024
HomeGame Developmentjava - Making a game loop to run multiple threads

java – Making a game loop to run multiple threads


I’m having a really hard time understanding Threads and their contents.

What I’d like to have is a main game object (which I have) and a loop, where I can measure the time it takes for two threads to finish doing their work.

So far I have two threads running like so, which are likely a poor idea:

public void run(){
    while(true){
         // do stuff, like update logics and draw to screen, in separate threads
    }
}

With which I get times measured from both threads, how long an iteration takes. But, I can’t seem to figure out how to get the times measured, combine them to get a total time of time spent per iteration.

After some modifications, I have, which seems to work, but I notice a drop in FPS, surely there’s a better way?

    public void Loop(){
        while(this.on){
            long time = System.nanoTime();
            this.updater = new Thread(this.canvas.logic);
            this.updater.start();
            this.drawer = new Thread(this.canvas);
            this.drawer.start();
            try{
                this.updater.join();
                this.drawer.join();
            }catch(InterruptedException e){

            }
            this.elapsed = (System.nanoTime() - time) * 0.0000001;
            System.out.println(this.elapsed);   
        }
    }

I’ll gladly provide more information as asked.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments