我有一个按钮,单击该按钮将导致线程 hibernate x 时间。
我希望线程从数组中获取时间量。
这应该发生:
- 单击按钮,选择第一个数字
- 线程 hibernate 然后做它的事情
- 完成需要执行的操作后,将选择下一个数字,然后重复该过程,直到没有更多的数字为止。
到目前为止我的代码是:
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/