Reputation: 515
I want to have a worker thread that can 1) send messages to the UI thread to change things in the GUI, and 2) receive messages from the UI thread to control the worker thread's behavior.
The first part is easy: Create a Handler in the UI thread and let the worker thread send messages to it. Works like a charm. The worker thread uses Thread.sleep() to perform an efficient delay, wakes up to "do things", sends an update to the UI thread's Handler, and repeats.
The next part is apparently difficult in Android. The worker thread must have a Looper to create a Handler so it can RECEIVE messages, and Looper.loop() is a blocking call which means the worker thread is now 100% committed to running the Looper and can't do the other things it is currently doing ("do things", send update message, sleep, repeat).
This implies I need a THIRD thread just to run the Looper for the worker thread, which is ridiculous. It should be possible to have the worker thread do useful things, AND process a message if and when one arrives. This yields an efficient system with the minimum number of thread objects and minimal overhead. I can't figure out how to implement this behavior in Android.
So... how does a worker thread support a Handler and do anything else useful? Thanks!
Upvotes: 0
Views: 2636
Reputation: 1006604
The worker thread uses Thread.sleep() to perform an efficient delay, wakes up to "do things", sends an update to the UI thread's Handler, and repeats.
It is possible this is the the right thing for your app, but since you decided not to describe your actual business problem, it is difficult to tell. For example, if you were writing an email client, and you needed to get control every 15 minutes to check for new email, your proposed solution would be unreliable.
The next part is apparently difficult in Android
Not especially.
The worker thread must have a Looper to create a Handler so it can RECEIVE messages
Only if you want to use a Handler
for that.
Java, as a programming language, has been around for a fairly long time. It has many means of inter-thread communication. Handler
, Looper
, and HandlerThread
wrap around these to offer one particular pattern. That pattern is not suitable for all scenarios, as you have noted. However, the rest of Java still exists, and many standard Java classes are part of Android that you can take advantage of.
So, for example, you could use a LinkedBlockingQueue
. Have the main application thread put commands onto that queue. Your worker thread, rather than using sleep()
, would use poll()
with a timeout to either pull an event from the queue or wake up after a period of time.
Upvotes: 2