Results 1 to 4 of 4
  1. #1
    dredd is offline Member
    Join Date
    Mar 2013
    Posts
    11
    Rep Power
    0

    Default how can I invoke a mehod while waiting for a thread to start?

    I have a method: broadcast(String msg) and I wan't to invoke the method
    only after an execution of run method in a class that extends Thread and holds the broadcast method
    that is to say I want that the broadcast method will wait until the call for start on the class that holds the broadcast method.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: how can I invoke a mehod while waiting for a thread to start?

    Can you post some of your code?

  3. #3
    dredd is offline Member
    Join Date
    Mar 2013
    Posts
    11
    Rep Power
    0

    Default Re: how can I invoke a mehod while waiting for a thread to start?

    I'm afraid it's a bit of a problem to post a snippet of my source code, because there is a lot going on there with dealing with different kinds of objects.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,544
    Rep Power
    5

    Default Re: how can I invoke a mehod while waiting for a thread to start?

    Here is an example of what I think you are trying to do?

    Java Code:
    public class ThreadTest {
       public static void main(String[] args) {
          BroadCastHolder foo = new BroadCastHolder();
          foo.start();
          while (!foo.active) {
             try {
                Thread.sleep(100);
             }
             catch (InterruptedException ie) {
             }
          }
          foo.broadcast("This is a test");
       }
    
    }
    
    class BroadCastHolder extends Thread {
       public volatile boolean active = false;
    
       public void run() {
          try {
             System.out.println("Run entered");
             sleep(2000);
             active = true;
             System.out.println(Thread.currentThread() + " marked as started");
             sleep(5000);
          }
          catch (InterruptedException ie) {
          }
          System.out.println(Thread.currentThread() + " exiting");
       }
    
       public void broadcast(String v) {
          System.out.println("CurrentThread = " + Thread.currentThread());
          System.out.println(v);
       }
    }
    However, please note that I am not an expert in thread programming and it can be difficult to debug and get correct. In a more complex application the volatile active flag may be insufficient to do what you want (i.e. synchronization and the use of wait/notify may be necessary. I would recommend you check out the java.util.concurrent API.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Replies: 2
    Last Post: 09-24-2011, 08:53 AM
  2. 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
  3. Thread not waiting for another with join()
    By DaCapitalist in forum Threads and Synchronization
    Replies: 1
    Last Post: 04-25-2011, 06:02 PM
  4. Using Thread waiting() method
    By nicoeschpiko in forum Advanced Java
    Replies: 1
    Last Post: 12-11-2010, 03:50 PM
  5. Thread RUNNABLE or WAITING
    By Pushkar in forum Threads and Synchronization
    Replies: 10
    Last Post: 01-14-2010, 01:36 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
  •