在Java中,您可以使用Thread類的sleep()方法來設(shè)置循環(huán)執(zhí)行的間隔時間。該方法會使當(dāng)前線程睡眠指定的毫秒數(shù)。
示例代碼如下:
public class Main {
public static void main(String[] args) {
// 設(shè)置循環(huán)次數(shù)
int loopCount = 10;
// 設(shè)置間隔時間,單位為毫秒
long interval = 1000;
for (int i = 0; i < loopCount; i++) {
// 執(zhí)行循環(huán)體的代碼
try {
// 線程睡眠指定的時間
Thread.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
在上面的示例中,循環(huán)體的代碼會被執(zhí)行10次,每次循環(huán)之間間隔1秒鐘。您可以根據(jù)需要自行調(diào)整循環(huán)次數(shù)和間隔時間。