顺序执行的线程 [英] Order of executed threads

查看:138
本文介绍了顺序执行的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在视觉上,我期待我的应用程序,以显示四个敬酒的顺序如下:

  1. createToast(开始延迟RandomCue法);
  2. createToast(延迟启动);
  3. createToast(延迟结束);
  4. createToast(年底推迟RandomCue法);

但是,生成的顺序是:

  1. createToast(开始延迟RandomCue法);
  2. createToast(年底推迟RandomCue法);
  3. createToast(延迟结束);
  4. createToast(延迟启动);

我的总体目标是有显示图像和改变以往3秒的节目。玩家可以preSS一个按钮,1.5秒的形象变化。因此,有两种方法,一种使用countdowntimer改变图像和另一个使用对应的ImageButton的onClick方法来改变图像。

我遇到的问题是在链路(从方法一个onclick方法中调用)提供的code应该改变形象,设置一个布尔值设置为false,等待1.5秒,然后变相同的布尔值回真。

虽然布尔值是true,改变图片的方法应该被忽略,但事实并非如此,我不知道为什么,但我认为它的东西做的code的要旨我下面创建。

所以这个问题我已经是在点击按钮时,图像变化预期,但它有时也是因为第一种方法不能识别玩家的反应,因此不应改变形象还没有变化太快。

 公共无效delayedRandomCue(最终长一){
    didPlayerRespond = TRUE;
    createToast(开始延迟RandomCue法);
    randomCue();
    螺纹延迟=新的Thread(){
        公共无效的run(){
            尝试 {
                createToast(延迟启动);
                视频下载(5000);
            }赶上(InterruptedException异常E){
                e.printStackTrace();
            }最后 {
                createToast(延迟结束);
                didPlayerRespond = FALSE;
            }
        }
    };延时启动();

    createToast(年底推迟RandomCue法);
}
 

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(最终长一)
}

 randomCue();
    螺纹cueThread =新的Thread(){
        公共无效的run(){
                尝试 {
                    而(fComm.fragmentGetTimerBool()){
                        如果(!didPlayerRespond){
                            如果(decTime> 1000){
                                视频下载(decTime);
                            } 其他 {
                                视频下载(1000);
                            }
                            decTime  -  = 50;
                            randomCue();
                        }
                    }
                }赶上(InterruptedException异常E){
                    e.printStackTrace();
                } 最后 {
                    saveScore();
                }
            //将变成图片的结束触发奖金sTimerOn = FALSE; fComm.fragmentScoreResponse(100);
            // createToast(红利完成所有可能的答案);
        }
    }; cueThread.start();
 

解决方案

 公共无效delayedRandomCue(最终长一){
        didPlayerRespond = TRUE;
 

出现这种情况首先

  createToast(开始延迟RandomCue法);
        randomCue();
        螺纹延迟=新的Thread(){
            公共无效的run(){
 

这发生在后台,异步。

 尝试{
                    createToast(延迟启动);
                    视频下载(5000);
                }赶上(InterruptedException异常E){
                    e.printStackTrace();
                }最后 {
 

这发生在后台,后异步运行,完成

  createToast(延迟结束);
                    didPlayerRespond = FALSE;
                }
            }
        };延时启动();
 

这是第二,因为它是与法的其他部分同步执行。

  createToast(年底推迟RandomCue法);
    }
 

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();

解决方案

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

这篇关于顺序执行的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆