0

I am writing an application. I came across scenario in such a way that, i have to create two threads, thread1 will be created first and thread2 will be created second. thread1 has to post in to the handler of the thread2. But as system is fast, before the time thread2 got created and handler got initialised , thread1 started posting the message objects to thread2. Due to which i am facing unexpected behaviour.

Please let me know how to wait in the thread1 for the thread2 to get started and handler is initialised. I tried polling mechanism, as it affects the system performance this fix is not getting accepted.

Tobias
  • 9,170
  • 3
  • 24
  • 30
Suman
  • 4,221
  • 7
  • 44
  • 64

5 Answers5

1

Just use something like that:

// @ Thread 1
synchronized( someMonitor ) {
  someMonitor.wait( /* time? */ );
}

// @ Thread 2
synchronized( someMonitor ) {
  someMonitor.notify();
}

So Thread 1 will wait until Thread 2 notifies. I would check before the wait if the condition is already set.

Tobias
  • 9,170
  • 3
  • 24
  • 30
  • Thread 1 is not guaranteed to start before Thread 2, so in that situation you could have an infinite wait. Better is to decouple them as much as possible without this kind of dependency (which does not really scale well for multiple producers or multiple consumers. – Mark Rotteveel Oct 11 '11 at 14:33
  • Have you read my last line? He should check if the condition is already set before waiting. – Tobias Oct 11 '11 at 14:35
  • Sorry, focussed on the code which did not have a wait condition. – Mark Rotteveel Oct 11 '11 at 14:48
  • This may work fine, but my situation was something like this http://stackoverflow.com/questions/4838207/how-to-create-a-looper-thread-then-send-it-a-message-immediately not exactly the same, used the code in this section to resolve the same. – Suman Oct 18 '11 at 06:25
0

Another way is to create thread 2 from thread 1 so that the queue, or whatever, that thread 2 uses for input is created before thread 1 gets around to posting stuff.

Rgds, Martin

Martin James
  • 24,453
  • 3
  • 36
  • 60
0

You need to rethink your design: It looks like you are working on a Producer/Consumer model, so you might want to use a (Blocking)Queue for the message exchange. Create it off both threads and pass it to both the Producer and Consumer on creation time. In that situation it doesn't matter which thread start to run first: if the producer then the queue starts to get filled, if the consumer, then it will wait until the producer has started as well.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Actually i am creating the queue in the thread, so before the thread is going to run state (In which new Handler() is being created), the thread which it created started posting in the next line of thread.start(). Due to which i was facing crash.So, this is not producer and consumer problem. I apologize if my description is not proper. – Suman Oct 18 '11 at 06:19
0

Your first option is to make the message queue global.

The second is to create thread2 from thread1.

Third is to have a synchronization object (Object.wait,notify,interrupt).

Still, I think the same as Mark does, that the problem is in your design, and you cant make up a good solution for this problem if it's the inherent design is not appropriate.

hege
  • 987
  • 4
  • 15