61321

While loop makes applet white screen and unresponsive

Question:

Here is my loop code (This is the only code relating to my loop):

while(true) { try { Thread.sleep(20); System.out.println("1"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

When I launch the applet, it white screens and I cannot close it unless I press the "Terminate" button in eclipse.

Answer1:

You're blocking the UI Thread with an infinite while loop. You don't say whether you're using an AWT or Swing applet, either way the result will be the same. If you're using a Swing applet, use a <a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html" rel="nofollow">Swing Timer</a>. If you're using the old heavyweight AWT, convert it to Swing and follow the previous advice.

Answer2:

As said you made an infinity loop with:

while(true){ //something }

There is no break; So why or what should stop the loop except of a thrown exception?

To see when a InterruptedException is thrown you should read the JavaDoc: <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.html" rel="nofollow">http://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.html</a>

Answer3:

You are hogging the applets EDT. You need to run your loop in another thread. Try adding Thread gameThread; as a variable and use<br />gameThread = new Thread() {<br /> public void run() {<br /> while (condition) {<br /> //code here<br /> }<br /> }<br /> } and then gameThread.start() both in your applet start method and gameThread.join() in your applet stop method.

Recommend

  • Custom thread pool supporting async actions
  • Why a class has only one destructor? [duplicate]
  • Cannot terminate threads
  • NSTimer and updating UI
  • GDB skips over my code!
  • Need help centering a div class on page
  • How does `std::terminate` know to treat `std::exception`s specially?
  • How to know if a JFrame is on screen in a multi screen environment
  • button in popup.html not working
  • Visual studio 2015 keystroke with mouse button
  • Confirm box return value from alertify plugin and jquery
  • Java : How to tint this PNG programmatically?
  • Wrapping text in spinner android
  • Receive mouse move even cursor is outside control
  • Any nice way to generate a timeline view of commits from subversion?
  • Local Development, Apache vs Developer - file permissions
  • Faster Way To Simultaneously Iterate Over Rolling Window Of Two Or More Numpy Arrays?
  • MeeGo Handset Emulator not starting on Windows 7
  • How to split circle in to the sectors in google maps?
  • Trying to get the char code of ENTER key
  • Django model inheritance, filtering models
  • java inputstream
  • Android fill_parent issue
  • Why value captured by reference in lambda is broken? [duplicate]
  • Android screen density dpi vs ppi
  • Optimizing database types to compact database (SQLite)
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • TFS: Get latest causes slow project reloading
  • Running a C# exe file
  • jquery mobile loadPage not working
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • php design question - will a Helper help here?
  • How to get icons for entities from eclipse?
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • AngularJs get employee from factory
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Reading document lines to the user (python)