Results 1 to 7 of 7
Thread: Thread Issue?
- 06-06-2012, 11:17 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
Thread Issue?
I think I have a thread issue, and I'm hoping someone can help me with it.
I create a thread and start it.
static Thread downloadScoresThread;
...
Runnable r2 = new DownloadScoresRunnable();
downloadScoresThread = new Thread( r2 );
downloadScoresThread.start();
DownloadScoresRunnable() contains run(), which itself contains three methods. (Two of these methods access databases, and the third method displays the results.)
After starting the above thread, the main thread opens a new JPanel to hold the results fetched from the databases.
This does NOT work; specifically, I fetch the results from the databases and open the JPanel, but the results are not displayed.
If I instead simply call the three methods in DownloadScoresRunnable() in lieu of the thread, everything works as desired.
Does anyone have any ideas why the thread is not doing what it is supposed to do?
- 06-06-2012, 11:21 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Re: Thread Issue?
a) It is doing what its supposed to...you need write it in such a way as to make it work as you need.Does anyone have any ideas why the thread is not doing what it is supposed to do?
b) Define "open the JPanel"
c) Be sure you pay attention to Swing's single threaded rule - for any updates that are not thread safe use a Swing worker or dispatch to the EDT using SwingUtilities.
d) Post an SSCCE to demonstrate
- 06-07-2012, 09:27 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
Re: Thread Issue?
Following on from doWhile's points, the usual route would be to launch your Swing app in the normal way (of which there are plenty of examples in the Oracle tutorials) and then have your Swing app fire off the aforementioned SwingWorker to go and fetch the data. While it is doing that you could have a simple "Loading..." graphic.
Please do not ask for code as refusal often offends.
- 06-14-2012, 04:36 PM #4
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
Re: Thread Issue?
Thanks for the help. Based upon your suggestions, I implemented the following solution which works.
Runnable r2 = new Runnable()
{
public void run()
{
DownloadScoresRunnable.getScores();
DownloadScoresRunnable.getCorrect();
ScoresPanel.loadScoresPanel();
}
};
javax.swing.SwingUtilities.invokeLater( r2 );
I also read the three articles on Swing and threads: "Threads and Swing", "Using a Swing Worker Thread", and "The Last Word in Swing Threads".
I am still a bit unclear on what is actually going on, and I think I have boiled my confusion down to a simple question.
In the main() of my program, I launch Swing with the following code,
javax.swing.SwingUtilities.invokeLater( new Runnable()
{
public void run()
{
createAndShowGUI();
}
} );
where createAndShowGUI() builds the GUI.
How many threads are running at this point? I would gather there are two threads: the one associated with the main() and one associated with the EDT for Swing. Is that right?
When I make another call to invokeLater(), is another thread started, or does the (already running) EDT handle this?
- 06-14-2012, 05:06 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
Re: Thread Issue?
invokeLater() causes whatever Runnable is in the call to be executed on the EDT.
The EDT (Event Dispatch Thread) is the GUI thread.
So we use invokeLater in the main() method there to ensure that our Swing stuff is all built on that thread, which is something Swing expects.
Now, by calling invokeLater and passing in your Runnable that loads the scores is simply causing all that work to be done on the EDT, which is what you should be avoiding if you don't want your GUI to lock up.
That's what the SwingWorker is for.Please do not ask for code as refusal often offends.
- 06-14-2012, 09:33 PM #6
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
Re: Thread Issue?
That's more clear. I'll recode it to use a SwingWorker. Thanks.
- 06-15-2012, 09:17 PM #7
Member
- Join Date
- Nov 2009
- Posts
- 90
- Rep Power
- 0
Similar Threads
-
Regular Expression issue and setName() method issue
By geforce in forum New To JavaReplies: 2Last Post: 01-30-2012, 03:33 AM -
Newbie issue: Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstJava
By jgro1976 in forum New To JavaReplies: 3Last Post: 09-28-2011, 02:42 AM -
Thread.sleep issue
By Savantx in forum New To JavaReplies: 6Last Post: 08-19-2011, 02:27 AM -
Main Thread not waiting for grand child thread to finish
By prashanthn in forum Threads and SynchronizationReplies: 3Last Post: 06-07-2011, 09:26 AM -
Java thread issue. booleans don't seem to work
By Fortu in forum New To JavaReplies: 2Last Post: 12-31-2010, 11:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks