非UI线程更新界面

非UI线程更新界面
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;
            }                        
          }
    }
}
非UI线程更新界面

非UI线程更新界面,布布扣,bubuko.com

非UI线程更新界面

上一篇:JavaScript将输入的数字金额转换成对应的中文大写金额


下一篇:java下static关键字用法详解