zoukankan      html  css  js  c++  java
  • 非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;
                }                        
              }
        }
    }
  • 相关阅读:
    pymysql模块操作数据库及连接报错解决方法
    lvs负载均衡
    redis(nosql数据库)
    zabbix
    shell正则表达式
    红帽CentOS7 密码破解
    shell基础及变量符号
    xshell连接虚拟机
    散列表与哈希算法学习笔记
    LeetCode-300 最长上升子序列
  • 原文地址:https://www.cnblogs.com/sandyflower/p/3628957.html
Copyright © 2011-2022 走看看