Jump to content

Swing program keep hanging

- - - - -

  • Please log in to reply
10 replies to this topic

#1
SandyTeo

SandyTeo

    Newbie

  • Members
  • PipPip
  • 22 posts
When I use my Swing program to run some files it will eventually get hang. Which even if I click the "X" panel (exit the program) on my JFrame it will not detect anything. However it will return normal when the program finish reading the files i had selected. How can I solve this problem ?

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
How do you start the processing of the file? With a button click?

#3
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
There's no code provided to verify this, but it sounds like you're loading several potentially involved files, and your being silly enough to load them on the Event Dispatch Thread, causing the Swing GUI to hang since that's the thread that maintains the GUI! You probably have a button or other GUI object that, when pressed or otherwise notified, starts what's on an ActionListener, and that ActionListener either calls the method/object that loads the files or performs the loading directly. Either way, you should offload this process onto a separate Thread and then update the GUI once the loading is done, providing a useful bar in the corner to indicate to the user when the loading is complete. This way the GUI doesn't freeze and will seem more responsive to the user.
Wow I changed my sig!

#4
SandyTeo

SandyTeo

    Newbie

  • Members
  • PipPip
  • 22 posts

wim DC said:

How do you start the processing of the file? With a button click?

Yup, I start to process the file by click the button.

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Well, just like ZekeDragon said. You should do that stuff in a separate thread:

public void actionPerformed(ActionEvent e){

  (new Thread(new Runnable(){

    public void run(){

        //code here

    }

  }).start();

}



#6
SandyTeo

SandyTeo

    Newbie

  • Members
  • PipPip
  • 22 posts

wim DC said:

Well, just like ZekeDragon said. You should do that stuff in a separate thread:

public void actionPerformed(ActionEvent e){

  (new Thread(new Runnable(){

    public void run(){

        //code here

    }

  }).start();

}


It said that " (new Thread(new Runnable(){" is not a statement. Did I done anything wrong ?

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I propably forgot a brace here:

public void actionPerformed(ActionEvent e){

  (new Thread(new Runnable(){

    public void run(){

        //code here

    }

  })[B][SIZE="4"])[/SIZE][/B].start();

}



#8
SandyTeo

SandyTeo

    Newbie

  • Members
  • PipPip
  • 22 posts
just wondering that is this called multithreading ?

public void actionPerformed(ActionEvent e){

  (new Thread(new Runnable(){

    public void run(){

        //code here

    }

  })).start();

}


#9
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Possibly, if you press the button 2 times in a row, there will be 2 threads running the same code. If you plan to allow that (you can disable this behaviour by disabling button onclick, and enabling when code is done) you gonna have to think about what can go wrong with multiple threads)

If you, for example, got a List which may not contain more than 5 items, as a business rule. The code would be like:

1. List myList;

...

5. public void someMethodAccessableByMultipleThreads(Item item){

6.   if(myList.size() < 5) {

7.      if( item.isValid() ) {

8.          myList.add(item);

9 .     }

10.  }

11.}

Now this code is not thread safe.
Because when the list currently contains 4 items, and then 2 threads start:
Thread A can be running first, executing lines 5 and 6, and before it starts line 7. Your operating system (or processor software) decides the thread now has to wait a bit, and another may run now.
If that other Thread is thread B, and it can execute 5,6,7,8,9 and 10. and thread B dies.
Now the OS / processor software, decides to continue with A, it has allready checked if the list doesn't contain 5 items, so it's not checking that again. It will just continue at line 7 and the rest of the code. Resulting in your list now containing 6 items.
While that was not allowed.

#10
SandyTeo

SandyTeo

    Newbie

  • Members
  • PipPip
  • 22 posts
My teacher ask me to use swingworker instead. But I dont understand a single stuff about that :( Btw if I use thread can I like disable about the button for user to press so that the user can't press it for multiple of times ?

#11
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
button.setEnabled(false / true) does that.

javadoc has 2 simple samples of the SwingWorker on top.
SwingWorker (Java Platform SE 6)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users