[英]Thread.sleep() alternatives
我希望此布爾值在5秒后從false更改為true(這是我當前的代碼)
checkUser = false;
loginMessage2 = "Error connecting to server.";
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
checkUser = true;
但是我不希望整個客戶端凍結,我不使用多線程或類似的方法
您可能需要考慮在要求執行5秒后執行的Timer
。 僅執行一次計時器。
這樣,您將獲得所需的內容,同時又不凍結應用程序。 計時器本身是一個不同的線程,因此它不會阻止您的應用程序。
Swing計時器教程: http : //docs.oracle.com/javase/tutorial/uiswing/misc/timer.html
一個非常非常簡短的示例:
ActionListener listener = new ActionListener(){
public void actionPerformed(ActionEvent event){
checkUser = true;
}
};
Timer checkUserTimer = new Timer(5000, listener); // the 5 second gap
checkUserTimer.start(); // start the timer.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.