java - How do I ensure another Thread's Handler is not null before calling it? -
My program threw a NullPointerException the second day when the handler was created on another thread to send a message to that thread Tried to do the handler created by another thread was not yet created, or the calling thread was not visible yet, despite the calling thread being started already on the other thread, it would only be very less Almost every test run is up Prahlad does not.
I was wondering what is the best way to avoid this problem with minimal problems and performance penalties. This program is a game and very sensitive, especially running once. So I try to avoid using synchronization after setup, for example, and at any time likes to avoid spinning on the variable.
Background:
Handler class in Android "to take action on a different thread than your own" Encryed A. "Documentation here:
Thread to handler Should be made where it will be used. Therefore, it is run by thread to make thread, making it in the manufacturer of thread, this is not an option.
When the handler is for a thread other than the UI thread, then the Loopper class can also be used:
The document uses two sections for this purpose Examples of:
class extends the LooperThread thread {public handler mHandler; Public run zero () {Looper.prepare (); MHandler = New Handler () {Public Zero Handle Message (Message msg) {// incoming incoming messages here}}}; Looper.loop (); }}
My very ugly solution currently looks like this:
public class LooperThread thread {public volatile handler mHandler; Public Ultimate Arrowboxing Quiz & lt; Object & gt; SetupComplete = new ArrayBlockingQueue & lt; Object & gt; (1); Public run zero () {Looper.prepare (); MHandler = New Handler () {Public Zero Handle Message (Message msg) {// incoming incoming messages here}}}; SetupComplete (); Looper.loop (); } Public Zero Wait FoSetupComplete () {While {true} {Attempt {setupComplete.take ()); Return; } Hold (interrupted e) e. {// Ignore and try again}}} Private zero setup full () {while {true} {try {full setup} (new object); Return; } Hold (interrupted ejection e) {// ignore and try again}}}}
with code in thread looking like this:
LooperThread otherThread = new LooperThread (); OtherThread.start (); OtherThread.waitForSetupComplete (); OtherThread.mHandler.sendEmptyMessage (0);
Is there a better solution? Thank you.
Preparing a luper
can block for a while, so I I think you're killing a condition where it takes some time to complete ready
, thus mHandler
is still undefined.
You may have your thread
extension, though you still have to wait to make sure that Looper
has started. There may be some work that is different, where you have the handler
defined separately, but using the Looper
of your custom thread.
May be.
Private Zero Set Up () {mHandlerThread = New Custom Thread ("foo", Process. THREAD_PRIORITY_BACKGROUND); MHandlerThread.start (); // Create our handler; This will be blocked until the luper is started mHandler = new custom handler (mHandlerThread.getLooper ()); // mHandler is now ready to use} Private class custom-spread spreads HandlerTrade {Public Zero Run ()} {// ...}} Private class expands custom handler handler {CustomHandler (Looper Lupper) {Super (Looper); } @ Override Public Wide Handess (Message Message) {// ...}}
Comments
Post a Comment