Em có viết một đoạn code đếm thời gian trong có chức năng như đồng hồ bấm giờ. Cho em hỏi liệu thời gian chạy theo đoạn code có đúng theo chuẩn bình thường không.Làm như thế nào để thời gian chạy đúng.Dưới đây là đoạn code của em:
public void run() {
try {
for (;;) {
try {
synchronized (this) {
if (resume) {
wait();
}
if (Stop) {
break;
}
}
} catch (Exception er) {
System.out.println(th.getName() + "Luồng bị ngắt!");
}
ms++;
Thread.sleep(1);
if ((ms % 100) == 0) {
ds++;
}
if (ms == 1000) {
ss++;
ms = 0;
}
if (ss == 60) {
mm++;
ss = 0;
}
strMS = mm + ":" + ss + ":" + ms;
RunTime.setText("Time: "+strMS);
//System.out.println(strMS);
}
} catch (Exception er) {
System.out.println("Event Time:" + er);
}
System.out.println("Stop Thread!!");
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}