Results 1 to 13 of 13
Thread: Multiple SwingWorkers
- 07-17-2010, 02:53 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
Multiple SwingWorkers
Im creating a swingworker thread after a button click of a gui to do some background work. From this new swingworker i have to start some new swingworker. this doesent work. the 2. thread never runs.
Here is some example code
After the button is clicked na Instance of Thread1 is created. The Thread1 starts and creates the object of Thread2, but thread 2 never starts running!!Java Code:class Thread1 extends Swingworker<Void,Void> { public Thread1() { execute(); } public void doInBackground() { new Thread2(); } } class Thread2 extends SwingWorker<Void,Void> { Thread2() { execute(); } public void doInBackground() { .....do some work here } }
Whats wrong with this code?
thx for any help
- 07-17-2010, 03:12 PM #2
Could you post some code that compiles. Your code doesn't even with the ... commented out.
- 07-17-2010, 03:17 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 28
- Rep Power
- 0
This was a bug (ID #6880336) that was introduced in java 6 update 18. In your example, the reason SwingWorker 2 doesn't start immediately is because your SwingWorker 1 hasn't finished yet. And in fact if your SwingWorker 1 waits on SwingWorker 2, then a deadlock occurs preventing all SwingWorkers from executing. This is the result of running all the workers on a single thread as stated in the bug report.
Technically the problem still exists before update 18, but you would have to create 10 simultaneous SwingWorkers before you notice anything.
- 07-17-2010, 03:19 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
- 07-17-2010, 03:22 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
Yes i checked it with a debugger! The execute() command ist reached but doInBackround is never called
So if thread1 inherites from Thread instead from Swingworker this would work correctly?This was a bug (ID #6880336) that was introduced in java 6 update 18. In your example, the reason SwingWorker 2 doesn't start immediately is because your SwingWorker 1 hasn't finished yet
- 07-17-2010, 03:30 PM #6
Member
- Join Date
- Jun 2010
- Posts
- 28
- Rep Power
- 0
Are you actually using the utilities of SwingWorker? That is, are you offloading the background task in doInBackground(), and updating a GUI in process() and/or done()? Changing it so that it extends thread instead will break that behavior.
- 07-17-2010, 03:37 PM #7
With minor changes to your code to get it to compile, I get the following output showing that the code does execute:
Thread1 constr
Thread1 doInB
Thread2 constr
Thread2 doInB
- 07-17-2010, 03:43 PM #8
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
Yes im only using the idDone() function to wait for the thread to get finished.
Could i use the getState() == Thread.State.Terminated function of the thread class instead?
Well i dont know why this example works and my programm doesnt. The example code is the exact same situation then in my programm.With minor changes to your code to get it to compile, I get the following output showing that the code does execute:
- 07-17-2010, 03:56 PM #9
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
k here is some working code that show the problem
if thread1 cant finish, cause its waiting or doing some other task thread2 will never start.Java Code:package charts; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class MyGui extends JFrame{ public MyGui() { this.setSize(200, 100); JButton button = new JButton("TestMe"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { new Thread1(); } }); this.add(button); } class Thread1 extends SwingWorker<Void,Void> { public Thread1() { System.out.println("Thread1 created"); execute(); } public Void doInBackground() { System.out.println("Thread1 running"); Thread2 thread2 = new Thread2(); while(true) { if ( thread2.isDone()) { break; } try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } return null; } } class Thread2 extends SwingWorker<Void,Void> { Thread2() { System.out.println("Thread2 created"); execute(); } public Void doInBackground() { System.out.println("Thread2 running"); return null; } } public static void main(String agrs[]) { MyGui gui = new MyGui(); gui.setVisible(true); } }
Thats what Maxideon has written before.
In your example, the reason SwingWorker 2 doesn't start immediately is because your SwingWorker 1 hasn't finished yet
- 07-17-2010, 04:29 PM #10
Member
- Join Date
- Jul 2010
- Posts
- 8
- Rep Power
- 0
I changed the Swingworker class to Thread. Now everything works finde. Thx for the help!
- 08-25-2010, 01:17 PM #11
Member
- Join Date
- Aug 2010
- Posts
- 2
- Rep Power
- 0
Hi Arne,
I am encountering the exact same problem. Can you please expand on what you mean? Possible put your solution into the example you gave earlier?
Thanks.
- 08-25-2010, 02:31 PM #12
Can't you just do
SwingWorker w1 = new SwignWorker(){
public void doInBackground() {
System.out.println("Thread 1running");
return null;
}
};
w.execute();
SwingWorker w2 = new SwignWorker(){
public void doInBackground() {
System.out.println("Thread 1running");
return null;
}
};
w2.execute();
- 08-26-2010, 02:54 AM #13
Member
- Join Date
- Aug 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Running multiple threads on multiple CPU cores?
By Dosta in forum Threads and SynchronizationReplies: 2Last Post: 09-19-2010, 03:48 PM -
Use multiple forms
By coco in forum Java AppletsReplies: 2Last Post: 06-14-2010, 05:14 AM -
Multiple window
By shoeb83 in forum New To JavaReplies: 1Last Post: 08-30-2009, 10:59 PM -
Multiple Inheritance
By mew in forum New To JavaReplies: 1Last Post: 12-01-2007, 10:04 PM -
multiple databases
By varunthecool in forum JDBCReplies: 2Last Post: 07-09-2007, 08:06 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks