Java for循环中设置停顿 逻辑代码中同样适用

for (Object object : jsonArray) {
    Thread.currentThread().sleep(1000);
    list.add(((JSONObject)object).get("Name"));
}

此处的停顿 适用于逻辑代码和循环 1000的单位为毫秒

停顿后提示一个未处理的异常

此时需要异常处理 

//抛出异常

throws InterruptedException

//或者try catch操作

try {
    Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
    e.printStackTrace();
}

猜你喜欢

转载自blog.csdn.net/weixin_40195422/article/details/84789856