java - 如何同时运行多个 "for"循环?

标签 java android

所以这是我在 Android 上的第一个应用程序,在我的一个方法中,我需要 4 个 for 循环同时运行。问题实际上是它们一个接一个地运行,而不是同时运行。这是我的方法: 编辑:

public void play() {
    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                    for (int i = 0; i < 8; i++) {
                        if (buttonArray0[i].isChecked()) { //Si le bouton d'indice i est actif, le son est joué.
                            soundPool.play(bongo, 1, 1, 1, 0, 1);
                        }
                    }
                    sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

    thread.start();

    Thread thread2 = new Thread() {
        @Override
        public void run() {
            try {
                    for (int i = 0; i < 8; i++) {
                        if (buttonArray1[i].isChecked()) { //Si le bouton d'indice i est actif, le son est joué.
                            soundPool.play(caisse_claire, 1, 1, 1, 0, 1);
                        }
                    }
                    sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

    thread2.start();

    Thread thread3 = new Thread() {
        @Override
        public void run() {
            try {
                    for (int i = 0; i < 8; i++) {
                        if (buttonArray2[i].isChecked()) { //Si le bouton d'indice i est actif, le son est joué.
                            soundPool.play(grosse_caisse, 1, 1, 1, 0, 1);
                        }
                    }
                    sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

    thread3.start();

    Thread thread4 = new Thread() {
        @Override
        public void run() {
            try {
                    for (int i = 0; i < 8; i++) {
                        if (buttonArray3[i].isChecked()) { //Si le bouton d'indice i est actif, le son est joué.
                            soundPool.play(cymbale, 1, 1, 1, 0, 1);
                        }
                    sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };

    thread4.start();
}

我做了一些研究,我想我明白你需要 java 程序中的线程才能同时运行许多东西,但我不知道如何在我的情况下实现线程,即使我尝试了几种方法和我在互联网上找到的东西。我需要一些建议才能完全理解它是如何工作的以及如何在我的案例中做到这一点。

谢谢大家。

最佳答案

您应该创建一个多线程程序

此方法将使该线程 hibernate 几秒钟,但除非执行此 for 循环,否则该线程不会进入另一个 for 循环。

Thread.sleep(1000);

试试这个

Thread thread = new Thread() {
@Override
public void run() {
    try {
        while(true) {
            //add your for loop here
            sleep(1000);
            handler.post(this);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
  }
};

thread.start();

如果 for 循环内有不同的计算,则应多次创建这些计算。

关于java - 如何同时运行多个 "for"循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60930567/

相关文章:

java - 如何在 Java NetBeans 中显示 CJK 字符和 GUI?

javascript - Android:将图像 URI 从 webview 网站发送到应用程序

android - 为更新的 bool 值添加流程

java - 如何使用Java API在NetSuite中完成搜索?

java - Android - Eclipse : Syntax error on token "(", 此标记后应有表达式

android - 导出(上传)apk 到 Play 商店崩溃了

android - Volley : TimeOutError

android - 按顺序显示android对话框

java - JAXB 中的 XmlAdaptar

java - Weblogic : The DBMS driver exception was: I/O Error: SSO Failed: Native SSPI library not loaded. 检查 java.library.path 系统属性