Results 1 to 6 of 6
Thread: Code executes after thread
- 11-26-2009, 05:13 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
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:
The problem is that this 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(); } }
Executes after the for loop has finished.Java Code:model.getProgressBar(type).setIndeterminate(true); model.getProgressBar(type).setStringPainted(true); model.getProgressBar(type).setString("Pagina's downloaden...");
I really can't find the problem.
Thanks in advance.
Glenn
-
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".
- 11-26-2009, 06:57 PM #3
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
I had a similar problem, you have to make a swingworker and run that in order to update your swing in this case.
- 11-26-2009, 09:15 PM #4
Senior Member
- Join Date
- Nov 2009
- Posts
- 150
- Rep Power
- 4
debug your code in an ide does it really executes then?
i think you should repaint your form to see it visibly.
- 11-27-2009, 01:56 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
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.
-
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
-
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 05:37 PM -
How to stop thread from being jumping off the code without executing it.....
By chiragkini in forum Threads and SynchronizationReplies: 6Last Post: 01-22-2009, 03:38 AM -
data from the main/GUI thread to another runnin thread...
By cornercuttin in forum Threads and SynchronizationReplies: 2Last Post: 04-23-2008, 10:30 PM -
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks