Hi Everyone,
Currently I have two threads, other than the UI thread, which need to
communicate with each other. Thread one needs to be able to send
messages directly to thread two, and vice versa. What is the best way
to implement this?
At the minute, my code looks like this:
runnable1 = new myRunnable();
runnable2 = new myOtherRunnable(runnable1);
runnable1.setOtherRunnable(runnable2);
sThread = new Thread(runnable1);
sThread.start();
Then, inside runnable1, I've got:
public void run() {
Thread otherThread = new Thread(runnable2);
.....*snipped code*.....
if (event){
runnable2.sendMessage(message);
otherThread.interrupt();
}
}
Thread two is in a constant sleep loop, until he is interrupted, at
which point he'll check his message queue and act on it.
-----------
I feel like there should be a better way to do this - this looks
particularly messy to me:
"runnable1 = new myRunnable();
runnable2 = new myOtherRunnable(runnable1);
runnable1.setOtherRunnable(runnable2);"
Add the fact I'm going to add a third thread that will also need to
send messages to and get messages from thread 2, which makes my head
near to exploding point thinking about it.
Should I be using callbacks/interfaces? Is there a good place to read
about this somewhere? I'm very new to this side of things really.
Many thanks,
~redders.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en