Thread: Java is broken

Page 1 of 2 12 LastLast
Results 1 to 10 of 14
  1. #1 Java is broken 
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Hey guys,

    I was working on something in Java and ran into this really weird problem.

    Take the following code:
    Code:
    public class Test{
    	public static void main(String[] args) throws Exception {
    		new Thread(() -> {
    			try {
    				run();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}).start();
    		
    		Thread.sleep(100);
    		stopRunning = true;
    		System.out.println("But it didn't end?");
                    System.out.println(stopRunning);
    	}
    
    	private static boolean stopRunning = false;
    	public static void run() throws InterruptedException {
    		while(!stopRunning) {
    		
    		}
    		System.out.println("Method ended.");
    	}
    }
    stopRunning is equal to true. The loop only runs when it is false, but why does not terminate? If I bang something into the body of the loop it will then terminate.
    But I don't want to add a redundant method call.

    I showed the problem to a friend and she said that it's probably an issue with the JVM? Is it?
    Reply With Quote  
     

  2. #2  
    Renown Programmer
    Greg's Avatar
    Join Date
    Jun 2010
    Posts
    1,179
    Thanks given
    260
    Thanks received
    1,012
    Rep Power
    2003
    Quote Originally Posted by Kiissmyswagb View Post
    Hey guys,

    I was working on something in Java and ran into this really weird problem.

    Take the following code:
    Code:
    public class Test{
    	public static void main(String[] args) throws Exception {
    		new Thread(() -> {
    			try {
    				run();
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}).start();
    		
    		Thread.sleep(100);
    		stopRunning = true;
    		System.out.println("But it didn't end?");
                    System.out.println(stopRunning);
    	}
    
    	private static boolean stopRunning = false;
    	public static void run() throws InterruptedException {
    		while(!stopRunning) {
    		
    		}
    		System.out.println("Method ended.");
    	}
    }
    stopRunning is equal to true. The loop only runs when it is false, but why does not terminate? If I bang something into the body of the loop it will then terminate.
    But I don't want to add a redundant method call.

    I showed the problem to a friend and she said that it's probably an issue with the JVM? Is it?
    Very interesting, only a print out in the while loop causes it to function semi-correctly, but even then the "method ending" is printed after the "but it didn't end?"

    I would guess that the reason is behind the timing with the jvm and that it is reaching the end of the main method before the end of the run method in the thread.
    Attached imageAttached image
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Quote Originally Posted by Greg View Post
    Very interesting, only a print out in the while loop causes it to function semi-correctly, but even then the "method ending" is printed after the "but it didn't end?"

    I would guess that the reason is behind the timing with the jvm and that it is reaching the end of the main method before the end of the run method in the thread.
    No, "method ending" does not get printed out. There is no timing issue going on.

    Here's my current findings:

    Under normal conditions the code fails to run on Eclipse.
    Debugging with Intelij works however running under normal conditions in Intelij it fails.


    Using javac in windows cmd and running it through a cmd, the code still fails.
    I have tested using Eclipse built in compiler and using java 1.8.0_121

    I updated Java and used jdk1.8.0_131 but it still did not work.
    Reply With Quote  
     

  4. #4  
    Renown Programmer
    Greg's Avatar
    Join Date
    Jun 2010
    Posts
    1,179
    Thanks given
    260
    Thanks received
    1,012
    Rep Power
    2003
    Quote Originally Posted by Kiissmyswagb View Post
    No, "method ending" does not get printed out. There is no timing issue going on.

    Here's my current findings:

    Under normal conditions the code fails to run on Eclipse.
    Debugging with Intelij works however running under normal conditions in Intelij it fails.


    Using javac in windows cmd and running it through a cmd, the code still fails.
    I have tested using Eclipse built in compiler using (java 1.8.0_121)

    I updated Java and used jdk1.8.0_131 but it still did not work.

    Fails as in doesn't work as expected or doesn't run at all?
    Attached imageAttached image
    Reply With Quote  
     

  5. #5  
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Quote Originally Posted by Greg View Post
    Fails as in doesn't work as expected or doesn't run at all?
    Didn't run as expected.

    Tested with jre7. Didn't print "method ended".

    Is there a logic problem or something I'm missing?

    Code:
    public class Test {
    
    	public static void main(String[] args) throws Exception {
    		new Thread(new Runnable() {
    
    			@Override
    			public void run() {
    				start();
    			}
    
    		}).start();
    
    		Thread.sleep(100);
    		stopRunning = true;
    		System.out.println("But it didn't end?");
    	}
    
    	private static boolean stopRunning = false;
    
    	public static void start() {
    		while (!stopRunning) {
    
    		}
    		System.out.println("Method ended.");
    	}
    
    }
    Reply With Quote  
     

  6. #6  
    Renown Programmer
    Greg's Avatar
    Join Date
    Jun 2010
    Posts
    1,179
    Thanks given
    260
    Thanks received
    1,012
    Rep Power
    2003
    Code:
    public class Test {
        public static void main(String[] args) throws Exception {
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    start();
                }
    
            }).start();
    
            Thread.sleep(100);
            stopRunning = true;
            System.out.println("But it didn't end?");
        }
    
        private static boolean stopRunning = false;
    
        public static void start() {
            while (!stopRunning) {
                System.out.print("");
            }
            System.out.println("Method ended.");
        }
    }
    "But it didn't end?
    Method ended."


    Code:
    public class Test2 {
        public static void main(String[] args) throws Exception {
            new Thread(new Runnable() {
    
                @Override
                public void run() {
                    start();
                }
    
            }).start();
    
            Thread.sleep(100);
            stopRunning = true;
            System.out.println("But it didn't end?");
        }
    
        private static boolean stopRunning = false;
    
        public static void start() {
            while (!stopRunning) {
                int i = 0;
                i++;
            }
            System.out.println("Method ended.");
        }
    }
    "But it didn't end?"

    Having a print out causes it to run (incorrect order though?), having anything else doesn't however
    Attached imageAttached image
    Reply With Quote  
     

  7. #7  
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Yep, if you add some redundant method call it will fix the problem. If you recall the method, the previous execution completes, however, the next execution will not.

    So after checking with multiple Java version, I guess it's a bug? Maybe it's the compiler or execution environment idk - I did a bug report. They will at least tell me if it's an issue or not.
    Reply With Quote  
     

  8. #8  
    Renown Programmer
    Greg's Avatar
    Join Date
    Jun 2010
    Posts
    1,179
    Thanks given
    260
    Thanks received
    1,012
    Rep Power
    2003
    Quote Originally Posted by Kiissmyswagb View Post
    Yep, if you add some redundant method call it will fix the problem. If you recall the method, the previous execution completes, however, the next execution will not.

    So after checking with multiple Java version, I guess it's a bug? Maybe it's the compiler or execution environment idk - I did a bug report. They will at least tell me if it's an issue or not.
    Yeah whatever it is, it's not doing what it's suppose to. Post if you get any answers I'm curious if there is an actual reason behind this behavior
    Attached imageAttached image
    Reply With Quote  
     

  9. #9  
    Registered Member

    Join Date
    Jan 2016
    Posts
    59
    Thanks given
    8
    Thanks received
    193
    Rep Power
    800
    Use a volatile modifier genius.
    Reply With Quote  
     

  10. Thankful user:


  11. #10  
    Registered Member
    Join Date
    Dec 2013
    Posts
    419
    Thanks given
    127
    Thanks received
    85
    Rep Power
    349
    Quote Originally Posted by apachenick View Post
    Use a volatile modifier genius.
    Yep, and now I know why.

    The byte code is the same (minus the volatile), volatile ensures it's ready from memory every time instead of cache. That is the reasoning.
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. [R E P] Java is not Reconised [R E P]
    By Wallace in forum Help
    Replies: 13
    Last Post: 06-20-2009, 03:25 PM
  2. [R E P] Java is not Reconised [R E P]
    By Wallace in forum Tutorials
    Replies: 12
    Last Post: 06-18-2009, 07:39 PM
  3. Replies: 10
    Last Post: 05-31-2009, 01:42 PM
  4. 'Java' is not recognized....
    By Mizr in forum Help
    Replies: 17
    Last Post: 04-14-2009, 06:57 AM
  5. 'Java is not recognized
    By Angelo in forum Help
    Replies: 5
    Last Post: 03-07-2009, 12:42 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •