Order of executed threads

前端 未结 2 417
太阳男子
太阳男子 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条回答
  •  -上瘾入骨i
    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");
        }
    

提交回复
热议问题