Results 1 to 5 of 5
  1. #1
    James2000 is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    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

    Java Code:
    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);
      }
    }
    Last edited by James2000; 05-11-2012 at 07:09 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

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

    There are a great many ways. Set a flag and wait for it to be set by the Runnable, use Thread.join(), create a Semaphore...

  3. #3
    James2000 is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

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

    Thank you for your reply. I have managed to RESOLVE this problem using the code below.

    Java Code:
    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
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

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

    Cross posted at Block thread until callback method has been called by other thread

    Be sure to tell the other forums that you have solved this.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,300
    Rep Power
    20

Similar Threads

  1. Help - Yet Another Paint() Called Twice thread
    By devdon in forum AWT / Swing
    Replies: 13
    Last Post: 04-11-2012, 01:30 PM
  2. Thread supposed to block keyevents
    By Fecoooo in forum Threads and Synchronization
    Replies: 6
    Last Post: 03-11-2012, 03:51 PM
  3. How to stop block thread while inputstream reading?
    By briliasm in forum Networking
    Replies: 0
    Last Post: 02-29-2012, 08:24 AM
  4. Trigger main thread method from secondary thread?
    By DigitalMan in forum Threads and Synchronization
    Replies: 8
    Last Post: 01-26-2010, 02:13 AM
  5. [SOLVED] Method from one thread called on another thread
    By Ypsilon IV in forum Threads and Synchronization
    Replies: 7
    Last Post: 04-24-2009, 02:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •