Results 1 to 5 of 5
- 05-11-2012, 07:04 PM #1
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
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.
- 05-11-2012, 07:39 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
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...
- 05-11-2012, 08:46 PM #3
Member
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
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); } }
- 05-11-2012, 08:55 PM #4
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.
- 05-11-2012, 09:46 PM #5
Re: Block thread until callback method has been called by other thread
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Help - Yet Another Paint() Called Twice thread
By devdon in forum AWT / SwingReplies: 13Last Post: 04-11-2012, 01:30 PM -
Thread supposed to block keyevents
By Fecoooo in forum Threads and SynchronizationReplies: 6Last Post: 03-11-2012, 03:51 PM -
How to stop block thread while inputstream reading?
By briliasm in forum NetworkingReplies: 0Last Post: 02-29-2012, 08:24 AM -
Trigger main thread method from secondary thread?
By DigitalMan in forum Threads and SynchronizationReplies: 8Last Post: 01-26-2010, 02:13 AM -
[SOLVED] Method from one thread called on another thread
By Ypsilon IV in forum Threads and SynchronizationReplies: 7Last Post: 04-24-2009, 02:07 PM


LinkBack URL
About LinkBacks

Bookmarks