Results 1 to 6 of 6
  1. #1
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default Code executes after thread

    Hi all,

    I have 2 threads: the main thread and a download thread, who downloads a page from the internet.

    Code:

    Java Code:
    		String numberOfPages = JOptionPane.showInputDialog("Aantal pagina's:");
    		if(numberOfPages != null) {
    			if(!numberOfPages.equals("0")) {
    				model.getProgressBar(type).setIndeterminate(true);
    				model.getProgressBar(type).setStringPainted(true);
    				model.getProgressBar(type).setString("Pagina's downloaden...");
    				if(type == 1) {
    					for(int i = 1; i <= Integer.valueOf(numberOfPages); i++) {
    						DownloadPage downloadPage = new DownloadPage(deTijd, i, deTijd.reverseLastPaper(deTijd.getLastPaper()));
    						Thread downloadPageThread = new Thread(downloadPage);
    						downloadPageThread.start();
    						model.getProgressBar(type).setString("Pagina's downloaden "+i+"/"+numberOfPages);
    						try {
    							downloadPageThread.join();
    						} catch (InterruptedException e) {
    							e.printStackTrace();
    						}
    					}
    The problem is that this code:

    Java Code:
    				model.getProgressBar(type).setIndeterminate(true);
    				model.getProgressBar(type).setStringPainted(true);
    				model.getProgressBar(type).setString("Pagina's downloaden...");
    Executes after the for loop has finished.
    I really can't find the problem.

    Thanks in advance.
    Glenn

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Are you sure that the downloads are being done off of the Swing EDT (event dispatch thread) and that the Swing calls are done on the EDT?

    If not, please read the Sun tutorial "Concurrency in Swing".

  3. #3
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    I had a similar problem, you have to make a swingworker and run that in order to update your swing in this case.

  4. #4
    dinosoep is offline Senior Member
    Join Date
    Nov 2009
    Posts
    150
    Rep Power
    5

    Default

    debug your code in an ide does it really executes then?
    i think you should repaint your form to see it visibly.

  5. #5
    bubbless is offline Member
    Join Date
    Mar 2009
    Posts
    81
    Rep Power
    0

    Default

    The download happens in a complete seperate thread, wich has nothing to do with the main thread, where I update the gui.
    As you see, the progressbar needs to be set to indeterminate, the pages need to be downloaded, and the progressbar needs to be set to complete.
    But the pages download first.

    It executes fine, execpt for the progressbar who updates after the other thread has finished.
    Also, repainting the progressbar didn't do anything.

    Edit.
    Fubarable, to answer your question, SwingUtilities.isEventDispatchThread() returned true in the main thread and false in the download thread.

    Another edit.
    I think the problem lies in the join method.
    The EDT joins the download thread and so it freezes until that thread has finished.
    Is there any other way to see if the thread has finished?
    Last edited by bubbless; 11-27-2009 at 02:41 PM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Don't use join!

    If you use a SwingWorker, you'll know that the thread is done as the done() method will be called. You could do a similar thing yourself but make it event-driven (such as with the Observer Model). Don't do join otherwise you lose the benefits of threading.

Similar Threads

  1. Difference between Thread.yield() and Thread.sleep() methods
    By Nageswara Rao Mothukuri in forum New To Java
    Replies: 12
    Last Post: 07-30-2010, 05:37 PM
  2. How to stop thread from being jumping off the code without executing it.....
    By chiragkini in forum Threads and Synchronization
    Replies: 6
    Last Post: 01-22-2009, 03:38 AM
  3. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 10:30 PM
  4. Replies: 0
    Last Post: 01-28-2008, 07:02 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
  •