如果没有线程,您可以在 while 循环中执行此操作:
long start = System.currentTimeMillis();
long current = start;
long threeSecondsAsMillis = 3 * 1000;
long oneSecondsAsMillis = 1000;
while (current < start + threeSecondsAsMillis) {
Thread.sleep(oneSecondsAsMillis);
current += oneSecondsAsMillis;
}
// continue your work
请注意,这是一个糟糕的解决方案,因为它会阻止您的 UI。更好的解决方案是使用 Eclipse Job:
Job job = new Job("") {
@Override
protected IStatus run(IProgressMonitor monitor) {
// note, that you are not in the UI-Thread anymore, but you must enable icon in the UI-Thread:
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
// enable icon
}
});
return Status.OK_STATUS;
}
};
job.schedule(3000);
如 greg-449 所述,您还可以使用 UIJob:
UIJob job = new UIJob("") {
@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
// enable icon
return Status.OK_STATUS;
}
};
job.schedule(3000);