wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Java program hangs when in infinite loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java program hangs when in infinite loop" Watch "Java program hangs when in infinite loop" New topic
Author

Java program hangs when in infinite loop

S. Shree
Greenhorn

Joined: Jun 25, 2008
Posts: 16
I have created a animation where balls fall from top to bottom. The balls are inside panels and the panels moves down. The top to down animation is done by run method of a class (Balls) extending Thread class. The animation starts at a click of button, but what happens is the buttons seems stuck and i cannot see the animation.

here is the code:

Balls[] b = new Balls[2];

while (true) {

b[0] = new Balls(0);
b[1] = new Balls(1);

b[0].start();
b[1].start();

// used the following lines so that the while loop waits before current threads are over
b[0].join();
b[1].join();
}

My overridden run() method is right, I load the ball image depending upon the instance variable numb and then use the repaint() method in the paintComponent() method.

Now, if I remove the while loop, it works perfectly fine, but I want it to run again and again and there is where it strucks. Please help!
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
usually you make a thread and the thread executes the while loop parallel to the application. what you are doing is executing the thread over and over in a while loop.

You should have to execute your thread(s) once and then in the thread do what is needed to be done "over and over again".

Justin Fox


You down with OOP? Yeah you know me!
S. Shree
Greenhorn

Joined: Jun 25, 2008
Posts: 16
Thanks for your suggestion Justin that really worked sweetly
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
Sweet! Usually I'm corrected lol.

Glad I could help ,

Justin
[ June 26, 2008: Message edited by: Justin Fox ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java program hangs when in infinite loop