Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Can anyone tell me why sleeping one thread is stopping my whole execution</p> </blockquote> <p>to better explain your Swing GUI is created on its own <em>special thread</em> separate from that which <code>main()</code> and other code will run in, this is done via creating your Swing components in the <code>SwingUtilities.invokeXXX</code> block (even if you have not done this your GUI will be run on a single thread called the <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html">initial thread</a>) . Now if you simply call <code>sleep</code> while on <code>Event Dispatch Thread</code> (or for that matter on the same <code>Thread</code>) it will wait for the call to <code>Thread.sleep</code> to finish. Now because all Swing events are processed on EDT we pause its execution by calling <code>sleep(..)</code> thus pausing the UI events from being processed and therefore GUI is frozen (until <code>sleep(..)</code> returns).</p> <p>You should not use <code>Thread.sleep(..)</code> on <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html"><code>Event Dispatch Thread</code></a> (or any <code>Thread</code> where sleep will cuase unwanted execution blocking), as this will cause the UI to seem frozen. </p> <p><a href="http://foxtrot.sourceforge.net/docs/freeze.php">Here</a> is a nice example which demonstrates exactly, this unwanted behavior caused by invoking <code>Thread.sleep(..)</code> on GUI's EDT.</p> <p>Rather use:</p> <ul> <li><p><a href="http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html">Swing Timer</a> for example:</p> <pre><code>int delay=1000;// wait for second Timer timer = new Timer(delay, new AbstractAction() { @Override public void actionPerformed(ActionEvent ae) { //action that you want performed } }); //timer.setRepeats(false);//the timer should only go off once timer.start(); </code></pre></li> <li><p><a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html">Swing Worker</a></p></li> </ul> <p>or if no Swing components are being created/modified:</p> <ul> <li><p><a href="http://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html">TimerTask</a></p></li> <li><p><a href="http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html">Thread</a>, you would then use <code>Thread.sleep(int milis)</code> (but thats last option in any case IMO)</p></li> </ul> <p><strong>UPDATE</strong></p> <p><code>Swing Timer</code>/<code>SwingWorker</code> was only added in Java 1.6, however, <code>TimerTask</code> and <code>Thread</code> have been around for alot longer sine Java 1.3 and JDK 1 repsectively, thus you could even use either of the 2 above methods and wrap calls that create/manipulate Swing components in <code>SwingUtilities/EventQueue#invokeXX</code> block; thats the way things used to be done :P</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload