cjayem13
cjayem13

Reputation: 913

Order of executed threads

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 delay");
  3. createToast("end of delay");
  4. createToast("end of delayed RandomCue Method");

However, the resulting order is:

  1. createToast("start of delayed RandomCue Method");
  2. createToast("end of delayed RandomCue Method");
  3. createToast("end of delay");
  4. createToast("start of delay");

My overall goal is to have a program that displays an image and changes ever 3 seconds. The player can press a button and the image changes for 1.5 seconds. Therefore, there are two methods, one to change picture using countdowntimer and another to change picture using onClick method corresponding to imagebutton.

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.

So the issue I have is when the button is clicked, the image changes as expected but it sometimes changes again too quickly due to the first method not recognizing that the player responded and thus shouldn't change image yet.

 public void delayedRandomCue(final long a){
    didPlayerRespond = true;
    createToast("start of delayed RandomCue Method");
    randomCue();
    Thread delay = new Thread() {
        public void run() {
            try {
                createToast("start of delay");
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                createToast("end of delay");
                didPlayerRespond = false;
            }
        }
    };delay.start();

    createToast("end of delayed RandomCue Method");
}

https://gist.github.com/cjayem13/d32446ceb8c6d9626c68#file-easyfragment-Java https://gist.github.com/cjayem13/d32446ceb8c6d9626c68

https://gist.github.com/cjayem13/d0a0b124dfe17666be25#file-easyfragment-Java https://gist.github.com/cjayem13/d0a0b124dfe17666be25

 onclick(){
delayedRandomCue(final long a)
}

 randomCue();
    Thread cueThread = new Thread(){
        public void run() {
                try {
                    while (fComm.fragmentGetTimerBool()) {
                        if(!didPlayerRespond) {
                            if (decTime > 1000) {
                                Thread.sleep(decTime);
                            } else {
                                Thread.sleep(1000);
                            }
                            decTime -= 50;
                            randomCue();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    saveScore();
                }
            // turn into end of all pics triggers bonus sTimerOn = false;  fComm.fragmentScoreResponse(100);
            //createToast("Bonus for completing all possible answers");
        }
    }; cueThread.start();

Upvotes: 0

Views: 76

Answers (2)

r2DoesInc
r2DoesInc

Reputation: 3771

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");
    }

Upvotes: 2

Anudeep Bulla
Anudeep Bulla

Reputation: 8548

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.

Upvotes: 2

Related Questions