Results 1 to 10 of 10
  1. #1
    barusk is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default [solved]Thread sync problems!

    I have two threads running in my program here. the main thread and one i call command(read) thread. When i hit a button i want the program to its job while i can still act with the gui. I want this because i have a halt button also. which will eventually halt my read() process and still print to a txtarea the result i was able to get before the halt.

    what i have now is i can interact with the gui but the txtarea's text is set before the read is finished. i have tried setting the priority of the main thread lower than the read thread, but the yeild statement just doesn't yeild long enough until the read is finished, and ends up setting the txtarea to "" because the read isnt finished but the main thread keeps going on to the txtarea.settext statement.

    i have also tried the wait/notify route, but this ends up freezing my gui until the read is finished. the textarea ends up being correct but i cant interact with my gui for the halt.

    anyone with an idea of how i can mix the two or soemthing different i need help please.

    thanks in advance
    ME
    Last edited by barusk; 04-30-2009 at 06:15 PM. Reason: need to mark as solved

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    You'll need to find every occurrence of the TextArea's setText() and make sure that it's in the right place, i.e. that it is safe to execute there. Code that updates the GUI should only be run in the Swing event thread, i.e. in an event handler or using SwingUtilities.invoke...()

    Have you looked at SwingWorker? It's designed to allow partial results to be returned by a thread.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    In case it's helpful, I have an example of SwingUtilities.invokeLater() you can take a look at, which does a roughly similar thing to what you're suggesting. The thing that people usually get wrong when they try and do this is that they transfer all their code to the invokeLater() rather than literally just the part that updates the UI.

    SwingWorker could help you make this logical split between the "work" and the UI update. (Personally, I don't think it buys you very much.)

  4. #4
    barusk is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    neil your page helped out a little bit. but i'm sure if i'll run into a problem. Your example there was an application. Mine is an applet. i know there are differences between the two, but im not sure off all of them. but i will give it a try. thanks guys.

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    No, there's essentially no difference between an applet and an app (specifically, make sure your applet subclasses JApplet).

  6. #6
    barusk is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    There is only one instance of the settext() so far Dog. I need to update the gui from my command thread, but because of stack overflow i can not do that. So i store what i need variables. The problem is my gui is updated prematurely before the command thread can fully finish.

  7. #7
    barusk is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    a goto statement would be great in this case. as long as it didnt freeze my gui like a loop would. to bad there is none in java.

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    OK, here's your problem. You are starting a thread and then updating the text area. Stop updating the text area.

    Instead, when your thread completes or is interrupted (by your halt button), have it do the following (I'm writing code from memory, so it's bound to be a bit off):

    final String text = *whatever is holding the text*;
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    textArea.setText(text);
    }
    });

    First, you have to put the text into a "final" variable in order to pass it into an anonymous class.

    Second, EventQueue is the class that controls UI processing. invokeLater() allows you to put a Runnable onto the queue for execution.

    Third, you create an anonymous inner class using new Runnable() {...} When the anonymous class is executed, it will update the text area.

    This is the standard pattern for updating UI components from another thread.

    You halt button should ideally set a flag on the thread's object. Be sure to synchronize when you update an object across threads. If the thread is doing something that "blocks", such as reading from an input stream, then you will have to use interrupt() on the thread. That will cause an Exception most of the time, so be sure to do a try catch. Also, if the thread loops, be sure to check isInterrupted() each time around the loop.

  9. #9
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    As previously stated, any code that updates the GUI (i.e. setText()) needs to be executed via the event queue.

    You need to use EventQueue.invokeLater() if using AWT, or SwingUtilities.invokeLater() if using Swing. If you need to wait for the update for some reason, use invokeAndWait().

    P.S. the only way you can feasibly get a stack overflow is by getting stuck in an endless method-calling loop. This usually happens in GUIs when calling various update methods from each other.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  10. #10
    barusk is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    thanks dog. i did some searching before you posted that, and i ended up doing exactly that. everything is now solved. I think you have ended up solving all my problems on here. you and steve are for sure guru's of java.

    anyway where i was gettin a stack overflow is when i made an instance of the main form n the command thread and an instance of command thread n the main form. noob mistake but i think im getting better at this with all your help.

    bout the only thing i have left to do for the app im working on is a progress monitor and it will be finished. taken 2 or 3 months but it works great.

    once again thanks to all who have helped in this process.

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, 06:37 PM
  2. project is out of sync with file system?
    By anthrax in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-04-2009, 06:19 PM
  3. Having problems with Thread
    By bozovilla in forum Threads and Synchronization
    Replies: 4
    Last Post: 10-16-2008, 12:36 AM
  4. JTable view/model sync problem
    By edcincy in forum AWT / Swing
    Replies: 0
    Last Post: 09-12-2008, 07:25 PM
  5. Replies: 0
    Last Post: 01-28-2008, 08: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
  •