Handler/Runnable delays producing events that are out of sync sometimes

二次信任 提交于 2019-12-06 05:04:43

purpleSquare runs before cleanup and things get screwed up

mainmethod() {
...
if (pointscored) {
    squaresglow();
    ...
    //delay so user can see the glow before the cleanup happens
    Handler-runnable
        cleanup();
    postdelayed
}
...
purpleSquare();
} 

You have a design flaw here. Think of Handlers as a queue of messages that will execute code "later" whenever the processor decides to process messages and postDelayed as an inexact way to stuff that message at the bottom of the queue. If you call postDelayed and you still have lines of code left in the current method to execute, chances are very good that those lines will execute before postDelayed messages are even received.

What you are trying to do is to make sure purpleSquare() gets called after the pointscored routine has done it's job, which may require waiting for it to finish. PostDelaying to the message queue is not what you should be doing in this case. What you should be using is a semaphore and a pointScored thread.

Consider the following code design:

final Runnable pointScoredTask = new Runnable() {
    public synchronized void run() {
        try {
            squaresglow();
            //...
            Thread.sleep(2500); //2.5 sec before cleanup occurs
            cleanup();
        } catch (InterruptedException e) {
        }
        notify(); //make sure we call notify even if interrupted
    }
};
void mainmethod() {
    //...
    if (bPointWasScored) {
        synchronized (pointScoredTask) {
            try {
                Thread psThread = new Thread(pointScoredTask,"pointscored");
                psThread.start(); //thread will start to call run(), but we get control back to avoid race condition
                pointScoredTask.wait(6000); //wait no more than 6 sec for the notify() call
            } catch (InterruptedException e) {
            } 
        }
        //if a point was scored, nothing past this line will execute until scoreglow has been cleaned up 
    }
    //...
    purpleSquare();
    //...
}

I know you'd rather avoid threads, but there are some things that just work much better when you use them. Try the above design and see if that works out the synchronization issues you were seeing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!