0

For the learning purpose I'm struggling to run a Thread WITHOUT using Timer,TimerTask,SwingWorker. Coming from PHP background EDT is something new & also hard for me to understand, I need your understanding for my situation.

I created/construct the following code, But I want to ask that is this Thread-safe? It makes me even more confuse because I ask this on another thread they just say that it's safe, but ugly . They just say the sentence, i would think that it isn't safe because that Thread.sleep() coming from the main thread, so why it's safe?

startButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent arg0) {

//========================================================================

       Thread worker = new Thread() {
           public void run() {

            // Simulate doing something useful.
            for(int i=0; i<=10; i++) {

             final int count = i;

             SwingUtilities.invokeLater(new Runnable() {
              public void run() {
               countLabel1.setText(Integer.toString(count));
              }
             });




             try {
              Thread.sleep(1000);
             } catch (InterruptedException e) {}
            } // end loop



            SwingUtilities.invokeLater(new Runnable() {
             public void run() {
              statusLabel.setText("Completed.");
             }
            });

           }
          };

          worker.start();


//==========================================================================       




   }
  });
Lii
  • 11,553
  • 8
  • 64
  • 88
  • 3
    Kind of. The problem you have is `SwingUtiltiies.invokeLater` will call your `Runnable` some time in the future, meaning the value of `count` will likely have changed beyond it's original value when you used `invokeLater`. You could use `invokeAndWait` which will wait for the `Runnable` to be executed before returning execution control back to the caller... – MadProgrammer Jul 10 '15 at 03:05
  • 3
    *"For the learning purpose I'm struggling to run a Thread WITHOUT using Timer,TimerTask,SwingWorker."* Unless you're learning 'how to work with arbitrarily restricted APIs' I wouldn't bother. Come to think of it, there's not much point in learning that either.. – Andrew Thompson Jul 10 '15 at 03:10
  • @AndrewThompson, you make the poin, somehow it doesn't make me sleep well in the night :D, it relates to http://stackoverflow.com/questions/30881263/recognizing-blocked-swing-edt , but I guess it's ok maybe in the future I will find it out. – Plain_Dude_Sleeping_Alone Jul 10 '15 at 03:18
  • @MadProgrammer, thanks so it's safe because of this invokeLater call the Runnable in the future well it makes sense too me, i will find out more about invokeAndWait , All these words are unfamiliar too me. :D – Plain_Dude_Sleeping_Alone Jul 10 '15 at 03:21
  • 2
    @hansf. It's "kind of" thread safe in the fact that the UI is been updated from a single thread (the UI), but because the value that the update is relying on will most likely have changed by the time the update is called, this has introduced a race condition – MadProgrammer Jul 10 '15 at 03:28

0 Answers0