Results 1 to 13 of 13
  1. #1
    Arne is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default 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
    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
      }
    }
    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!!

    Whats wrong with this code?

    thx for any help

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default

    Could you post some code that compiles. Your code doesn't even with the ... commented out.

  3. #3
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    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.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Arne View Post
    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!!

    Whats wrong with this code?
    Did you check that for a fact? Add a debug printline as the first line of your method:

    Java Code:
      public void doInBackground() {
        System.out.println("running Thread2 ...");
        .....do some work here
      }
    kind regards,

    Jos

  5. #5
    Arne is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    Yes i checked it with a debugger! The execute() command ist reached but doInBackround is never called
    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
    So if thread1 inherites from Thread instead from Swingworker this would work correctly?

  6. #6
    Maxideon is offline Member
    Join Date
    Jun 2010
    Posts
    28
    Rep Power
    0

    Default

    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.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default

    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

  8. #8
    Arne is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    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?

    With minor changes to your code to get it to compile, I get the following output showing that the code does execute:
    Well i dont know why this example works and my programm doesnt. The example code is the exact same situation then in my programm.

  9. #9
    Arne is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    k here is some working code that show the problem

    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);
    	}
    	
    }
    if thread1 cant finish, cause its waiting or doing some other task thread2 will never start.

    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

  10. #10
    Arne is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    I changed the Swingworker class to Thread. Now everything works finde. Thx for the help!

  11. #11
    Brendan is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default

    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.

  12. #12
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default

    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();

  13. #13
    Brendan is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default

    PhQ,

    We are trying to prove concurrency, there is no concurrency in the example you just gave.

Similar Threads

  1. Running multiple threads on multiple CPU cores?
    By Dosta in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-19-2010, 04:48 PM
  2. Use multiple forms
    By coco in forum Java Applets
    Replies: 2
    Last Post: 06-14-2010, 06:14 AM
  3. Multiple window
    By shoeb83 in forum New To Java
    Replies: 1
    Last Post: 08-30-2009, 11:59 PM
  4. Multiple Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-01-2007, 11:04 PM
  5. multiple databases
    By varunthecool in forum JDBC
    Replies: 2
    Last Post: 07-09-2007, 09:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •