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 ?
10 replies to this topic
#1
Posted 08 May 2011 - 05:17 AM
|
|
|
#2
Posted 08 May 2011 - 07:33 AM
How do you start the processing of the file? With a button click?
#3
Posted 08 May 2011 - 08:13 AM
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
Posted 08 May 2011 - 08:37 AM
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
Posted 08 May 2011 - 08:39 AM
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
Posted 08 May 2011 - 08:58 AM
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
Posted 08 May 2011 - 09:01 AM
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
Posted 10 May 2011 - 07:22 PM
just wondering that is this called multithreading ?
public void actionPerformed(ActionEvent e){
(new Thread(new Runnable(){
public void run(){
//code here
}
})).start();
}
#9
Posted 14 May 2011 - 02:10 AM
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:
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.
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
Posted 14 May 2011 - 04:28 AM
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
Posted 14 May 2011 - 05:52 AM
button.setEnabled(false / true) does that.
javadoc has 2 simple samples of the SwingWorker on top.
SwingWorker (Java Platform SE 6)
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


Sign In
Create Account


Back to top









