package com.caterSys.Thread; import java.text.SimpleDateFormat; import java.util.Date; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; public class TimeThread extends Thread { private Display display; private Label label; //要更新的控件 private boolean isStop = false; public TimeThread(Display display, Label label) { this.display = display; this.label = label; } public boolean isStop() { return isStop; } public void setStop(boolean isStop) { this.isStop = isStop; } @Override public void run() { // TODO Auto-generated method stub final SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss"); format.setLenient(false); Runnable runnable=new Runnable() { @Override public void run() { synchronized (this) { label.setText(format.format(new Date())); } } }; while (!isStop) { try { Thread.sleep(1000); display.asyncExec(runnable); //每隔1秒让主线程异步执行刷新时间线程 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); isStop=true; } } } }