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();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章