java - 从数组中获取下一个 int

标签 java multithreading

我有一个按钮,单击该按钮将导致线程 hibernate x 时间。

我希望线程从数组中获取时间量。

这应该发生:

  1. 单击按钮,选择第一个数字
  2. 线程 hibernate 然后做它的事情
  3. 完成需要执行的操作后,将选择下一个数字,然后重复该过程,直到没有更多的数字为止。

到目前为止我的代码是:

private int[] intArray = new int[]{4, 3, 6, 2, 5};

public void myMethod() {
    try {
        int x = 0;
        for (int i = 0; i < intArray.length; i++) {
           x = intArray[i];
        }

        Thread.sleep(x);

        // my custom things happen here after the thread sleep

    } catch (InterruptedException ex) {
        Logger.getLogger(RoutePanel.class.getName()).log(Level.SEVERE, null, ex);
    }

}

最佳答案

sleep 和“自定义事物”应该位于 for 循环内。

private int[] intArray = new int[]{4, 3, 6, 2, 5};
public void myMethod() {
  try {
    int x = 0;
    for (int i = 0; i < intArray.length; i++) {
      x = intArray[i];
      Thread.sleep(x);
      // my custom things happen here after the thread sleep
   }
  } catch (InterruptedException ex) {
    Logger.getLogger(RoutePanel.class.getName()).log(Level.SEVERE, null, ex);
  }
}

关于java - 从数组中获取下一个 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36226560/

相关文章:

java - 两个不同时区的时差

java - Intellij 中的 Scala 配置

java - 错误 : No functional channel service provider found. 尝试添加对 grpc-okhttp 或 grpc-netty Artifact 的依赖

java - Spring 中的自定义电子邮件服务 - 需要帮助改进

java - 如何构建多线程套接字服务器

c++ - 超线程性能比较

java - SortedLinkedList 删除两个节点之间的头部、尾部方法

java - 使用 CommandLineRunner spring boot 创建模式

java - 阻止java程序打开线程

python - 为什么我的使用 Queue 、 threading.Thread 和 subprocess 的多线程 python 脚本如此不稳定