Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: How to make a program to "sleep" until my Thread ends

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to make a program to "sleep" until my Thread ends

    I have a problem with a program that I try to make. Inside a JFrame class, I start one Thread. That thread checks something and return a boolean as true or false. Inside the JFrame class I want to check that boolean, and do actions according to the results of the thread.
    Now my question is how can I make my JFrame to wait untill the Thread ends?

    If you need some code, please ask, but I hope you understand where my problem is.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to make a program to "sleep" until my Thread ends

    Try using Thread.join()

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    83
    My Mood
    Cynical
    Thanks
    3
    Thanked 9 Times in 9 Posts

    Default Re: How to make a program to "sleep" until my Thread ends

    Quote Originally Posted by helloworld922 View Post
    Try using Thread.join()
    No, no, don't use Thread.join() in a Swing application on the event thread as this risks tying up the Swing event thread putting your entire GUI to sleep and making it completely unresponsive. There are better ways to solve this including using a PropertyChangeListener and listen for the one thread to complete. A SwingWorker is great for this since it comes automatically bundled with PropertyChangeSupport.

    So create a SwingWorker to do your background threading, and listen to the worker by adding a PropertyChangeListener, and resume what you're doing when its "state" property returns the SwingWorker.StateValue.DONE.

    Links:
    Last edited by Fubarable; July 12th, 2012 at 12:15 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to make a program to "sleep" until my Thread ends

    Quote Originally Posted by Fubarable View Post
    No, no, don't use Thread.join() in a Swing application on the event thread as this risks tying up the Swing event thread putting your entire GUI to sleep and making it completely unresponsive. There are better ways to solve this including using a PropertyChangeListener and listen for the one thread to complete. A SwingWorker is great for this since it comes automatically bundled with PropertyChangeSupport.

    So create a SwingWorker to do your background threading, and listen to the worker by adding a PropertyChangeListener, and resume what you're doing when its "state" property returns the SwingWorker.StateValue.DONE.

    Links:
    fair point. I would concur that this is the best route to follow, even if it does requires some re-write of other code.

  5. #5

    Default Re: How to make a program to "sleep" until my Thread ends

    It's better to use event publisher/subscriber like Fubarable suggested.

  6. #6
    Junior Member
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to make a program to "sleep" until my Thread ends

    If u dont wana updgrade to jdk1.6 ,using thread.join() is recommended..
    I am spawning some threads in my program to perform db related tasks..
    and in order to make my program wait till those threads dies.. i created threads in thread array,started all threads and then iterated the array to call thread.join

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to make a program to "sleep" until my Thread ends

    Quote Originally Posted by joshiparag123456 View Post
    If u dont wana updgrade to jdk1.6 ,using thread.join() is recommended..
    Read post #3, which is the correct advice in this situation (and even better, uses proper grammar - something recommended when posting to a technical forum). It leaves out explicitly mentioning SwingUtilities, which can be used prior to the introduction of SwingWorker (1.6)

Similar Threads

  1. How can I make a JDialog to make main Frame to "sleep"
    By piulitza in forum AWT / Java Swing
    Replies: 1
    Last Post: May 11th, 2012, 08:00 AM
  2. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 07:15 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. Replies: 1
    Last Post: August 2nd, 2011, 05:13 PM