Results 1 to 13 of 13
  1. #1
    jokop is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default why this simple java application do infinete while loop?

    why this simple java application do infinete while loop?
    thanks very much
    niv jokop

    Java Code:
    class DualSync {
    
    
    public boolean busy=true;
    
      public synchronized void store() {
    	  System.out.println("before");  
    	  while (busy) ;		  
    	  System.out.println("after");
      }
    
      public synchronized void read() {
    	  
      }
    }
    
      public class SyncObj{
    
      public static void main(String[] args) throws InterruptedException {
        final DualSync ds = new DualSync();
        new Thread() {
        	public void run() {
                	ds.store();
          }
        }.start();
        Thread.sleep(5000);
        ds.busy=false;
        ds.read();
        System.out.println("end main");
      }
     
      
    } ///:~
    Last edited by jokop; 02-03-2011 at 11:00 AM. Reason: rephrasing subject

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default

    What do you mean? This program reaches the end of main for me just fine. Are you sure you're waiting 5 whole seconds before deciding that it has locked up?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jokop is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    thanks for the reply
    yes I'm pretty sure it shows only the word "before" and then halts. (it dont terminate)
    maybe its related to the fact that Im using Ubuntu OS and java-6-openjdk (what ever that means,
    as I have regular updates that I agree to install whenever Im requested to do so.)

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default

    I find it hard to accept that your OS or JDK would behave so differently. Are you certain you're waiting the full 5 seconds? Are you sure this is the code you're running?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    jokop is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default here is a screenshot of my eclipse notice the red square

    here is a screenshot of my eclipse
    notice the red square that signals the application is still running.
    notice also that the console output is only "before"
    thanks for your interest
    niv jokop
    Attached Thumbnails Attached Thumbnails why this simple java application goes into dead lock?-screenshot.jpg  

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    It deadlocks for me as well. IMO this is a very poor way to write code, a better design would be to utilize the wait()/notify() methods. As to why, the fact that it works for KevinWorkman has me a bit stumped but I can guess is its an OS issue - see volatile below (possible local caching involved?), but logically the synchronized store() method continually acquires a lock on the object, thus the main thread can never access the busy variable. I mentioned this is poor code, but an interesting example as to thread deadlocking. Remove the synchronized on store, and it still locks (in a different place). Use a synchronized statement to lock ds prior to setting the flag to true and it still locks. Do the above and mark the boolean as volatile and it works. I will let the original poster look up what volatile means...
    Last edited by doWhile; 02-03-2011 at 03:48 PM.

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default

    Weird. It definitely exits for me. I'm using Windows XP and Java 1.6.0_22. It exits successfully both in eclipse and from the command line.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default

    Also- this doesn't really explain why it works for me and not you, but what happens when you make your busy variable volatile?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Make your variable 'busy' volatile.

    kind regards,

    Jos

    edit: *sigh* not again, too slow again ... ;-)
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    jokop is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    doWhile
    why it cant access the locked objects variables. as I understand only synchonized methods of the locked object cannot be invoked by the main thread (while the object is locked with the "inner" thread).
    regards
    niv jokop

  11. #11
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Quote Originally Posted by jokop View Post
    doWhile
    why it cant access the locked objects variables.
    See Typical use of the volatile keyword
    and
    Intrinsic Locks and Synchronization (The Java™ Tutorials > Essential Classes > Concurrency)

    In this case its not about locking per se, its about local caching of a variable.

  12. #12
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    KevinWorkman's and JosAH's advice will probably "fix" your problem, but this is a horrible design. You are probably eating an entire CPU doing nothing but checking the status of 'busy'. The right approach would be to get rid of the 'busy' variable altogether, and use a 'wait' instead. In place of setting 'busy' to false, use a notify.

    Java Code:
    class DualSync {
    
      private boolean busy = true;
      private boolean hasStore = false;
    
      public synchronized void store() {
        System.out.println("before");  
    
        hasStore = true;
        notify();
    
        while(busy) {
          try {
            wait();
          } catch (InterruptedException iex) {}
        }
    
        System.out.println("after");
      }
    
      public synchronized void read() {
        while(!hasStore) {
          try {
            wait();
          } catch (InterruptedException iex) {}
        }
    
        busy = false;
        notify();
      }
    }
    
    public class SyncObj{
    
      public static void main(String[] args) throws InterruptedException {
        final DualSync ds = new DualSync();
    
        new Thread() {
        	public void run() {
               ds.store();
          }
        }.start();
    
        ds.read();
    
        System.out.println("end main");
      }
     
      
    } ///:~
    (haven't tested it, but should work ;p )
    Last edited by toadaly; 02-04-2011 at 05:24 AM.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by toadaly View Post
    KevinWorkman's and JosAH's advice will probably "fix" your problem, but this is a horrible design. You are probably eating an entire CPU doing nothing but checking the status of 'busy'. The right approach would be to get rid of the 'busy' variable altogether, and use a 'wait' instead. In place of setting 'busy' to false, use a notify.
    True, a busy loop burns your cpu but there are even nicer solutions than wait() and notify(); there's a bunch of great classes in the java.util.concurrent package that can handle most (if not all) of those multi threaded problems and they take away the wait, synchronize, notify scheduling from the use of those classes. Those classes have been written by Doug Lea and were already with us as a separate package years ago. It's a mature technology and should be used imho.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Dead threads
    By Onra in forum New To Java
    Replies: 2
    Last Post: 12-19-2010, 01:26 PM
  2. Restarting dead threads
    By DC200 in forum Threads and Synchronization
    Replies: 3
    Last Post: 12-03-2010, 10:09 PM
  3. Simple Java application methodology
    By vishantgarg in forum Forum Guides
    Replies: 5
    Last Post: 11-03-2009, 03:32 PM
  4. New simple application using a simple database
    By webbusiness23 in forum New To Java
    Replies: 9
    Last Post: 08-03-2009, 02:55 AM
  5. Converting simple Java Application to Applet
    By jrohde in forum Java Applets
    Replies: 1
    Last Post: 07-25-2009, 07:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •