0

I'm experiencing some incredibly weird behavior in a java application.

Here is the loop in question

    while(true)
    {
        System.out.println("feed message queue is... " + feedMessageQueue.peek());
        //feedMessageQueue.peek();

        if (this.feedMessageQueue.peek() != null ){
            for (String message : feedMessageQueue)
            {
                System.out.println("Sending message to client: " + message);

                out.println(message);
                this.feedMessageQueue.remove();

            }

        }

    }//end while

Now, I originally tried for my if condition if (feedMessageQueue.size() > 0) and that would not work.

However, this condition works (and the for loop is executed) ONLY WHEN I run the print statement above just under the while loop.

I thought perhaps it had to do with the peek() method, so I tried calling that instead of the print statement.

I'm really moreso just curious why I'd be seeing behavior like this? How could the print statement affect the following conditional?

Thanks for any thoughts!

user207421
  • 305,947
  • 44
  • 307
  • 483
ethand320
  • 145
  • 2
  • 8
  • This should not happen. Seems that you're doing something wrong. Provide more context: how `feedMessageQueue` is created and how it's filled. Is `this.feedMessageQueue` and `feedMessageQueue` the same object? Probably you have local var with the same name? – Tagir Valeev Mar 03 '16 at 06:07
  • THanks for your reply, exactly my thoughts! feedMessageQueue is being filled by another class (it is a public priorityqueue in the shown class). So another class is calling feedMessageQueue.add("stringhere"); – ethand320 Mar 03 '16 at 06:07
  • I suspect problem with your `feedMessageQueue`. Are your sure, its not empty? – Tirath Mar 03 '16 at 06:07
  • Also what do you mean by "would not work"? Throws exception? And why do you need `if`? You can just remove it. If `feedMessageQueue` is empty, the `for` loop will not be executed. And do you have everything in the same thread? Note that `PriorityQueue` is not thread-safe. – Tagir Valeev Mar 03 '16 at 06:08
  • Yes, what's interesting is that when i include the System.out.println, it prints the correct contents of feedMessageQueue, then the conditional is true, and that prints the correct contents of feedMessagQueue. This is a multithreaded application so perhaps something has gotten messed up due to multiple starts/stops during testing? – ethand320 Mar 03 '16 at 06:09
  • If you are filling the `feedMessageQueue` in another thread, then *any kind* of error or incorrect behavior might occur as `PriorityQueue` is not thread-safe. Try `PriorityBlockingQueue` instead. – Tagir Valeev Mar 03 '16 at 06:10
  • You should post the code from the thread/class that fills the queue too – MartinS Mar 03 '16 at 06:12
  • Guys, thank you for the thoughts, I've removed the if statement completely as you are correct it is not needed. After restarting the terminals i was running the clients in, the issue has disappeared. I will also switch the object to a PriorityBlockingQueue, thanks again! – ethand320 Mar 03 '16 at 06:14
  • It's all wrong anyway. You shouldn't be removing while iterating except via the iterator, and you shouldn't be iterating at all, otherwise you don't get the priority order. – user207421 Mar 03 '16 at 06:24

0 Answers0