Results 1 to 7 of 7

Thread: Thread Issue?

  1. #1
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default 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?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Thread Issue?

    Does anyone have any ideas why the thread is not doing what it is supposed to do?
    a) It is doing what its supposed to...you need write it in such a way as to make it work as you need.

    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

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default 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.

    ** This space for rent **

  4. #4
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default 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?

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default 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.

    ** This space for rent **

  6. #6
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: Thread Issue?

    That's more clear. I'll recode it to use a SwingWorker. Thanks.

  7. #7
    newbie123 is offline Member
    Join Date
    Nov 2009
    Posts
    90
    Rep Power
    0

    Default Re: Thread Issue?

    The SwingWorker approach worked beautifully. Thanks doWhile and Tolls.

    I now see the importance of running the time-consuming code in a worker thread and NOT on the EDT.

    Problem solved!

Similar Threads

  1. Regular Expression issue and setName() method issue
    By geforce in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 03:33 AM
  2. Replies: 3
    Last Post: 09-28-2011, 02:42 AM
  3. Thread.sleep issue
    By Savantx in forum New To Java
    Replies: 6
    Last Post: 08-19-2011, 02:27 AM
  4. Main Thread not waiting for grand child thread to finish
    By prashanthn in forum Threads and Synchronization
    Replies: 3
    Last Post: 06-07-2011, 09:26 AM
  5. Java thread issue. booleans don't seem to work
    By Fortu in forum New To Java
    Replies: 2
    Last Post: 12-31-2010, 11:14 PM

Posting Permissions

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