Order of executed threads

前端 未结 2 416
太阳男子
太阳男子 2021-01-16 18:46

Visually, I\'m expecting my app to show four Toasts in the following order:

  1. createToast(\"start of delayed RandomCue Method\");
  2. createToast(\"start of
相关标签:
2条回答
  • 2021-01-16 18:57
    public void delayedRandomCue(final long a){
            didPlayerRespond = true;
    

    this happens first

     createToast("start of delayed RandomCue Method"); 
            randomCue();
            Thread delay = new Thread() {
                public void run() {
    

    This happens in the background, asynchronously.

                    try {
                        createToast("start of delay");
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }finally {
    

    This happens in the background, after asynchronously running and completing

                        createToast("end of delay");
                        didPlayerRespond = false;
                    }
                }
            };delay.start();
    

    This comes second because it is executed synchronously with the rest of the method.

     createToast("end of delayed RandomCue Method");
        }
    
    0 讨论(0)
  • 2021-01-16 19:14

    This doesn't answer you question about the order of the toasts, but are you sure , when you say

    The problem I'm running into is the code provided in the link (method called from within an onclick method) is supposed to change the image, set a bool value to false,wait 1.5 seconds, and then change the same bool value back to true.

    While the bool value is true, the method that changes the pictures is supposed to be skipped but that's not the case and I don't know why but i think it's something to do with the code in the gists I created below.

    that this is the case?

    It looks more like , if the boolean is set to false, the other method that changes the pictures needs to be skipped, and as long as it is true, it needs to periodically change the pictures.

    Try that, and maybe your image won't change very quickly after the user did trigger the onClick.

    0 讨论(0)
提交回复
热议问题