1

So in this section of code I have, I want to essentially tell the GUI to disable the button and bring up a pop-up window when no threads are running anymore (i.e. the method called has finished).

public void actionPerformed(ActionEvent event) 
{
    String command = event.getActionCommand();

    //If btnConvertDocuments is clicked, the FileConverter method is called and the button is then disabled [so as to prevent duplicates].
    if (command.equals("w"))
    {
        new Thread(new Runnable() 
        {
            public void run() 
            {
                FileConverter fc = new FileConverter();

            }
         }).start();
        if (Thread.activeCount() == 0)
        {
            btnConvertDocuments.setEnabled(false);
            //Validation message ensuring completion of the step.
            JOptionPane.showMessageDialog(this, "Step 1 Complete!", "Validation", JOptionPane.INFORMATION_MESSAGE);
        }
    }

Why does that if (Thread.activeCount() == 0) never seem to get called? Is that not what I want to be doing in order to accomplish my objective? Thank you in advance for any input!

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
This 0ne Pr0grammer
  • 2,632
  • 14
  • 57
  • 81
  • As an alternative to using threads directly, you might want to check out [`ExecutorService`](http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html) and [`Future`](http://download.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html). – Daniel Pryden Aug 02 '11 at 22:09

2 Answers2

2

There are many threads that are running when you run a Java program (for example, the main thread :) and look here) if you want to check the state of a thread, use the getState() method (just remember to assign the thread to a Thread variable:

        Thread t = new Thread(new Runnable() 
        {
            public void run() 
            {
                FileConverter fc = new FileConverter();

            }
         });
        t.start();
        if (t.getState().equals(Thread.State.TERMINATED) ) { ... }

Looking more into your question, you could call the join method as well, as it will block the current thread until t is done (or until timeout).

Community
  • 1
  • 1
MByD
  • 135,866
  • 28
  • 264
  • 277
2

that's about Concurency in Swing, better would be wrap you BackGroung Task to the SwingWorker, very nice example by @Hovercraft Full Of Eels, or by implements Executor here

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • +1 this is something i didn't take into account (and that's a terrible mistake...) – MByD Aug 02 '11 at 22:25
  • What exactly would be the difference in terms of using this executor as opposed to the join I just used? – This 0ne Pr0grammer Aug 02 '11 at 22:33
  • @This 0ne Pr0grammer with Executor(s) you can play with MultiThreading, but with using SwingWorker is most important that your GUI didn't freeze during BackGround Task(s), all JComponents are accesible, but in other hand I must say that you are going possible way too, but litte bit complicated for workaround, look here http://stackoverflow.com/questions/6051755/java-wait-cursor-display-problem/6060678#6060678 – mKorbel Aug 02 '11 at 22:47