3

I am working on a java multi-threaded application on Android.

Thread 1: Listener Thread. keep listening on a socket, fires 2nd thread for some action

Thread 2: Action Thread. short lived. get fired from first thread, do a simple action and dies.

Thread 1 does not wait on Thread 2, as it goes back to listening after firing Thread 2.

Can I execute a method in Thread1 (say thread2ActionCompleted()) from Thread2 just before it ends.

For some reason I cannot use static method or static variable in Thread 1

I saw some similar threads using ExecutorService, but couldn't find a good example if it will work in my situation.

ajitk
  • 63
  • 1
  • 8
  • Please edit your question and associated tags with the language you are asking about. – Daddy Aug 16 '13 at 18:03
  • Just pass a reference to Thread1 to Thread2. – Sotirios Delimanolis Aug 16 '13 at 18:05
  • Nah, you're using `Android`. FYI, you should edit the tags instead of commenting. It is always desired that the OP update the question with more information and not sprinkle it in comments. – Tim Bender Aug 16 '13 at 18:09
  • Once upon I already answered similar question, check this: http://stackoverflow.com/a/13934591/1891118 , there is sample with ```Executor``` – Oleksii K. Aug 16 '13 at 19:06

4 Answers4

1

Looke like your problem is simple enough, here is pseudo code.

Thread1 {

 run() {
    //check condition
    //If condition meets create Thread Thread2
    //set parent in Thread2
    //start Thread2
 }

  Method m1 {

  }
}

Thread1 keep running, you can start thread 2 base on your condition.

Thread2 {
   Thread1 parent;
   //When done call parent.m1
}

Cheers !!

Sachin Thapa
  • 3,559
  • 4
  • 24
  • 42
  • I want to know when thread2 completes in thread1 and call some post-processing method in thread1. – ajitk Aug 16 '13 at 18:18
  • How do I pass flagDone from thread1. I do not want thread1 to keep check if thread2 is done. thread2 should call a post-processing method in thread1 once its done. – ajitk Aug 16 '13 at 18:36
  • @ajitk - This could be done in so many ways, but have edited pseudocode again, trying to keep it simple. – Sachin Thapa Aug 16 '13 at 18:51
  • Sachin - I cannot create a second instance of Thread1. Thread1 is continuously listening for events on an established communication channel (Socket Stream to a C++ module). Sharing this channel will open up more synchronization issues. – ajitk Aug 16 '13 at 22:34
1

Just an update: I ended up passing parent thread instance as a parameter to the constructor of child thread. Worked like a charm and any code that I wanted to be access only once at a time, I put in synchronize block.

Parent Thread: Thread1
Child Thread: Thread2

class Thread1 extends Thread implements Runnable {
  ...
  Thread2 t2 = new Thread2(this);
  new Thread(t2, "ChildThread").start();
  ...
}

class Thread2 extends Thread implements Runnable {
 ...
 Thread1 t1;
 Thread2 (Thread1 t) {
   t1 = t;
 }
 ...
 t1.CallAnyMethod();
 ...
}
ajitk
  • 63
  • 1
  • 8
0

This is a potential candidate for an implementation of the observer pattern. Thread 2 would be able to notify all of its observers (in particular, Thread 1) when it is finished doing it's work.

For example:

Thread 1 implements Observer.

Thread 2 implements Observable.

When thread 2 finishes, it notifies all observers that it has finished. This would tell thread 1 that thread 2 is done.

Surveon
  • 729
  • 5
  • 12
  • I would encourage you to look at the sample that Wikipedia provides. It is a relatively straight forward implementation of the pattern. – Surveon Aug 16 '13 at 18:09
  • Observable is not an interface. Hence this will not work in my case. (This is the error I get: The type Observable cannot be a superinterface of Thread1.java; a superinterface must be an interface). This basically means a Thread cannot be Observable. It can be an Observer though. – ajitk Aug 16 '13 at 22:25
0

What you probably want is a Future. Basically, thread2 will place its result an a thread-safe holder object which is shared with thread1. When thread1 wants to know what happened with thread2 it can check the Future object.

This could be accomplished using a ExecutorService to manage thread2's lifecycle and submitting a Callable to the ExecutorService.

Tim Bender
  • 20,112
  • 2
  • 49
  • 58
  • Sounds good. I am going to explore this option and will let you know if it works for me. – ajitk Aug 16 '13 at 18:37