• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

infinite loop

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am referencing an object r of an applet class called Register in my Tracker applet class. After an action event in Tracker i call the init() method of r. thats fine. i then add it to a JFrame and show it, causing a form to be displayed to the user. the user fills the form and submits. at this point i want the username to be allocated to a JLabel in my Tracker applet and the JFrame holding the the Register object to disappear. The problem occurs when the user tries to submit the form. I am getting an infinite loop:

while(!myBoolean) //this is satisfied until i explicitly say so.
{ if(!r.getStatus()) //this is NOT satisfied until the user submits.
{ myLabel.setText(r.getUserName());
myFrame.hide();
myBoolean = true;
}
}

when the user submits the status is changed so r.getStatus() should now be false causing the loop to enter the above if block. Can anyone help me out? its been bugging me for days.
gratefully,
zapf
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any loop along the lines of:

will take up 100% of the JVM's CPU allocation and lock out any other threads/processes. I suspect what is happening is the JVM doesn't have a chance to set your boolean values to anything else because it is locked in that loop. Try inserting a Thread.sleep(500); at the bottom of your while loop. This will give the other threads a chance to run
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yup, i agree with Fletcher.

But i still dont see the reason for u to use the infine loop. Why dont you use the actionListener to get the data when it is entered?
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I call "The Busy/Wait Anti-Pattern" - there are other names for this anti-pattern.

Generally, a busy/wait suggests the need to use the wait/notify mechanism (in Java at least, this anti-pattern is not restricted to Java). The workaround (or hack depending on your perception) of sleeping the thread at intervals has further negative consequences.

There are many good texts on this topic - I suggest "Taming Java Threads, Allen Holub". Doug Lea's book has had positive review comments as well (but I haven't read it).
 
zack chancery
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shashank,
i cant use the action listener in this case because my Tracker class is doing most of the work. i am not instantiating a Tracker object so when i click submit im not able to say Tracker.doSomething().
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you want something like this?

 
zack chancery
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not sure if that will work ryan or maybe the while clause should be myBoolean instead of !myBoolean in your code. if so wouldnt i still get this infinite loop. ill take a look at both. thanks!
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic