0

Ok, this is an odd one. Basically I am creating a playlist of sorts so background music in my game will play after the previous song as finished, etc. If I remove the System.out.println(music.isPlaying()); then the block of code within the if statement never gets called, even when music.isPlaying() is in fact false . But if I add the print statement before it, then magically it works as it should. But this is only with the print statement, I can't just send the boolean to some empty method or set a boolean variable to it, it only works when sent to the console. However, for obvious reasons, I would not like it's truth value to be spammed to the console. Does anyone know why this is acting out this way and how I could fix it?

    while (true) {
        System.out.println(music.isPlaying()); // Only works with this here
        if (!music.isPlaying()) {
            System.out.println("Changing song");
            currentSongIndex++;
            if (currentSongIndex >= musicFiles.size())
                currentSongIndex = 0;
            music = musicFiles.get(currentSongIndex);
            music.play();
        }
    }

I'm not sure if this has any affect on this but this loop is being called on a 2nd thread, as my game is ran on it's own thread. If for any reason this might be the reason, let me know and I can post my multi-threading code.

BossLetsPlays
  • 121
  • 1
  • 14
  • 2
    If this is a multithreading question, you should share the threading code. – Mick Mnemonic Feb 24 '16 at 21:21
  • "the block of code within the if statement never gets called" what makes you think this is the case? – Raedwald Feb 24 '16 at 21:27
  • Will it work without `System.out` if you change the `isPlaying` variable (I assume there is such a variable) in the `Music` class to `volatile`? – Mick Mnemonic Feb 24 '16 at 21:35
  • Shouldn't there be a minor sleep on this thread to free up some time for other executions to take place given that it's coded as an infinite loop? It possibly works when you put the print statement in for this same reason, effectively pausing the current thread's execution briefly. – ManoDestra Feb 24 '16 at 21:45
  • @MickMnemonic Well I wasn't entirely sure this was a multithreading issue or not, and it seems making my playing boolean volatile solved the problem, should I still post the multi-threading code despite it being solved? – BossLetsPlays Feb 24 '16 at 22:12
  • That's not necessary anymore, because the duplicate question describes the problem and also why calling `System.out` has the same effect as declaring the variable `volatile`. – Mick Mnemonic Feb 24 '16 at 22:34

2 Answers2

1

You need to declare the boolean variable that Music.isPlaying() is using as a volatile like this:

volatile boolean playing;

You can read more about what the volatile keyword does on Javamex.

Jonah Haney
  • 521
  • 3
  • 12
  • Thanks! This made it work, though I'm not sure why adding that print also made it work, but I'm glad to get rid of it – BossLetsPlays Feb 24 '16 at 22:11
  • It's because the `System.out.println()` method is synchronized, meaning something along the lines of: it has to update the variables across all threads before it can work. – Jonah Haney Feb 24 '16 at 22:19
0

First of all, please post your threading code.

In addition, you could save a boolean to the value being printed, in order to preserve it for future function calls. You can do this inside of your print statement (I know that you said you can't save it otherwise.

Make sure that your methods are "synchronized" so that they are not competing for the same resources.

Enusi
  • 101
  • 1
  • 5