Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Java strange thread behavior

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Java strange thread behavior

    I've got a thread with a loop, here's my code:

    public class UpdateThread extends Thread
    {
    	public void run()
    	{
     
    		while (Game.isActive)
    		{
     
    		}
    		System.out.println("Thread stopped");
    	}
    }

    The Game.isActive is a static boolean that's true while the game is running. It's false when the game stopped.

    The code above doesn't work. But why does this code work?

    public class UpdateThread extends Thread
    {
    	public void run()
    	{
     
    		while (Game.isActive)
    		{
                            System.out.println("Thread running");
    		}
    		System.out.println("Thread stopped");
    	}
    }

    It's very strange. But it would be very inefficient and annoyoing to output something while running the thread, please help!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java strange thread behavior

    There's probably a better option than a while( true ) loop to begin with, but the reason the first version isn't doing anything is because of the magic optimizing done by the Java compiler (I think that's where it's done). You can learn more about it by searching 'java empty while loop' or similar search about code that really doesn't do anything.

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java strange thread behavior

    Actually, this isn't my real code it's just an example. My real code has lots of object updating stuff in it but without an output it doesn't seem to stop.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java strange thread behavior

    The code above doesn't work. But why does this code work?
    Define doesn't work. Does the while loop get executed? Does the while loop continue indefinitely? Is the field Game.isActive declared as volatile? In situations such as this, it helps to provide an SSCCE which can be run and tested by others so that everyone is on the same page.

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java strange thread behavior

    I don't want to show all my code just for this problem. But the while loop continues indefinitely.

    --- Update ---

    But strangely when in debug mode in eclipse and suspending the program and stepping over or into the code, it will stop.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java strange thread behavior

    I don't want to show all my code just for this problem.
    I didn't ask for all your code, I asked for an SSCCE

    But the while loop continues indefinitely.
    I'll ask again, is Game.isActive declared as volatile? If not, please try it

  7. The Following User Says Thank You to copeg For This Useful Post:

    Bingo90 (October 19th, 2013)

  8. #7
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java strange thread behavior

    Ah thanks, the volatile worked

  9. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Java strange thread behavior

    Glad you got it working

Similar Threads

  1. My Java program gets a strange error
    By 119mikkel in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 12th, 2013, 11:47 AM
  2. Duplicating default behavior of unix sort
    By dreamerBoy in forum Java Theory & Questions
    Replies: 1
    Last Post: May 31st, 2013, 07:10 PM
  3. JButtons behavior in ActionListener
    By QuincyOrsot in forum Object Oriented Programming
    Replies: 3
    Last Post: April 26th, 2012, 03:00 PM
  4. Strange errors with java.nio.file
    By jnewb in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 4th, 2012, 02:37 PM
  5. Making a living from Java in strange ways :)
    By Squiffy in forum Member Introductions
    Replies: 3
    Last Post: November 21st, 2011, 05:10 AM