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

Thread: Block thread until callback method has been called by other thread

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Block thread until callback method has been called by other thread

    Hi All,

    Please can you advise best way to block or suspend an application thread, until another thread has called a method in the application's class.

    Thank you for your time and help,
    Best regards,
    James

    public class ThreadTester {
      public static void main(String[] args) {
        new ThreadTester().application();
      }
     
      public void application() {
        for (int i=0; i<10; i++) {
          Thread t = new Thread(new Process(this, i));
          t.start();
          /* wait here until serviceSearchCompleted() has been called by Process */
        }
      }
     
      public void serviceSearchCompleted(int number) {
        System.out.println("Completed search " + number);
        /* notify application() method to continue beyond 'waiting point' */
      }
    }
     
     
     
    class Process implements Runnable {
      private ThreadTester threadTester;
      private int number;
     
      public Process(ThreadTester threadTester, int number) {
        this.threadTester = threadTester;
        this.number = number;
      }
     
      public void run() {
        startSearchServices();
      }
     
      private void startSearchServices() {
        System.out.println("Started search " + number);
        try {
          Thread.sleep(1000);  //...seaching...
        } catch (InterruptedException ie) {}
        threadTester.serviceSearchCompleted(number);
      }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Block thread until callback method has been called by other thread

    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Block thread until callback method has been called by other thread

    I have managed to RESOLVE this problem using the code below.

    public class ThreadTester {
      private Object synchObj = new Object();
     
      public static void main(String[] args) {
        new ThreadTester().application();
      }
     
      public void application() {
        for (int i=0; i<10; i++) {
          Thread t = new Thread(new Process(this, i));
          t.start();
          synchronized (synchObj) {
            try { synchObj.wait(); } catch (InterruptedException ie) {}
          }
        }
      }
     
      public void serviceSearchCompleted(int number) {
        System.out.println("Completed search " + number);
          synchronized (synchObj) {
            synchObj.notify();
          }
      }
    }
     
     
     
    class Process implements Runnable {
      private ThreadTester threadTester;
      private int number;
     
      public Process(ThreadTester threadTester, int number) {
        this.threadTester = threadTester;
        this.number = number;
      }
     
      public void run() {
        startSearchServices();
      }
     
      private void startSearchServices() {
        System.out.println("Started search " + number);
        try {
          Thread.sleep(1000);  //...seaching...
        } catch (InterruptedException ie) {}
        threadTester.serviceSearchCompleted(number);
      }
    }

  4. #4

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Block thread until callback method has been called by other thread

    You should utilize the concurrency package eg. with a Lock or a Barrier

Similar Threads

  1. My First thread
    By shyandeep chatterjee in forum Member Introductions
    Replies: 0
    Last Post: April 18th, 2012, 05:06 AM
  2. What is this thread for exactly?
    By javapenguin in forum Computer Support
    Replies: 2
    Last Post: July 2nd, 2011, 10:37 AM
  3. Repaint method in Thread not moving String
    By javapenguin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 10th, 2011, 10:33 PM
  4. Using Thread waiting() method
    By nicoeschpiko in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2010, 11:55 AM
  5. Thread Hangs at split method.
    By kailasvilaskore in forum Threads
    Replies: 1
    Last Post: November 26th, 2010, 12:13 PM