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 3 of 3

Thread: Waiting for another thread to finish

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Waiting for another thread to finish

    I have a method with two threads (1 &2) which run at the same time. The current thread must stop right after the ‘thread2.start()’ statement until thread2 & thread1 are finished. There appears to be multiple approaches to do this but I find them confusing. Can anybody point to me a simple approach to accomplish this? Join does not appear to work since thread 1 & 2 must run at the same time. Thanks in advance




    Runnable storm = new Storm();
    Runnable UpdatePB = new UpdatePB();

    //these two threads must run at the same time
    thread1 = new Thread(storm);
    thread2 = new Thread(UpdatePB);

    thread1.start();
    thread2.start();


    //code must stop here until thread2 is finished


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Waiting for another thread to finish

    Why do you say join doesn't work? What have you actually tried?

    Recommended reading: Joins (The Java™ Tutorials > Essential Classes > Concurrency)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Waiting for another thread to finish

    try {
    thread2.join();
    }
    catch(InterruptedException e) {
    System.out.println("Exception caught : " + e);
    }

    It works.

Similar Threads

  1. Runtime.Exec() not waiting
    By PrinceSendai in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: June 9th, 2011, 02:12 PM
  2. Waiting for a Second JButton Click
    By Firebert in forum Java Theory & Questions
    Replies: 0
    Last Post: March 16th, 2011, 08:15 PM
  3. Using Thread waiting() method
    By nicoeschpiko in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2010, 11:55 AM
  4. Waiting until condition
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: October 22nd, 2010, 09:24 AM