`

SWT之org.eclipse.swt.SWTException: Invalid thread access

阅读更多

    SWT中初学者不经意会碰到这个异常,有时候很明确是创建多个Display对象引起的,但是为什么呢?

 

Q: Why do I get the error "org.eclipse.swt.SWTException: Invalid thread access"?
A: In SWT, by definition the thread that creates the Display is a UI-thread. This thread is responsible for reading and dispatching events from the operating system event queue, and invoking listeners in response to these events. Listener code is executed in the UI-thread. This makes an SWT application generally quite responsive, behaving like most other operating system programs. However, any long operation, when executed by a listener, will run in the UI-thread and prevent it from reading and dispatching events, thus hanging the application.

If a listener has a large amount of work to perform, instead of performing that work in the UI-thread, it can fork a separate thread so the UI-thread can continue dispatching events. If the other thread needs to execute code that accesses an SWT object, such as changing the string in a label, there is a concurrency issue. At the very least, some kind of synchronization is necessary to prevent the operating system or SWT from crashing, hanging or behaving unpredictably.

SWT implements a single-threaded UI model often called apartment threading. In this model, only the UI-thread can invoke UI operations. SWT strictly enforces this rule. If you try and access an SWT object from outside the UI-thread, you get the exception "org.eclipse.swt.SWTException: Invalid thread access". Different operating systems have different rules governing threads, UI components and synchronization. Some use a single-threaded UI model like SWT. Others allow only one thread at a time in the window system library, controlling access through a global lock. This type of multi-threaded UI model is often called free threading. Currently, in order to be simple, efficient and portable, SWT is apartment threaded.

To allow background threads to perform operations on objects belonging to the UI-thread, the methods syncExec(Runnable runnable) and asyncExec(Runnable runnable) of Display are used. These are the only methods in SWT that can be called from any thread. They allow a runnable to be executed by the UI-thread, either synchronously, causing the background thread to wait for the runnable to finish, or asynchronously allowing the background thread to continue execution without waiting for the result. A runnable that is executed using syncExec() most closely matches the equivalent direct call to the UI operation because a Java method call always waits for the result before proceeding, just like syncExec().

The following code sets the text of a label from a background thread and waits for the operation to complete:

display.syncExec(
  new Runnable() {
    public void run(){
      label.setText(text);
    }
  });

但貌似new Display涉及不到多线程:
Applications which are built with SWT will almost always require only a single display. In particular, some platforms which SWT supports will not allow more than one active display. In other words, some platforms do not support creating a new display if one already exists that has not been sent the dispose() message.
在建立swt的程序时一般总是只需要一个display对象。但也有特别的情况,有的平台不支持swt创建多个活动的display对象。也就是说,如果 已存在一个display对象,但是给它没有发送disose()消息来销毁它,有的平台是不支持再创建一个新的display对象。

SWT实现的是单线程UI模型,在这个模型里,只有UI线程(创建首个Display对象的线程)才能调用UI操作,如果从UI线程外访问SWT对象就会 得到异常"org.eclipse.swt.SWTException: Invalid thread access".
但我想重复new Display对象也未必和多线程挂得上勾,查看Display源码及一些资料,应该基于所创建新Display对象之线程已经存在的判断:
if (Displays [i].thread == thread) SWT.error (SWT.ERROR_THREAD_INVALID_ACCESS);



 
  • 大小: 7 KB
分享到:
评论

相关推荐

    org.eclipse.paho.android.service-1.1.1-sources.jar

    Android的MQTT消息推送接收所需jar包,修复了MqttAndroidClient会出现throw new IllegalArgumentException Invalid ClientHandle...implementation files('libs/org.eclipse.paho.android.service-1.1.1-sources.jar')

    org.xml.sax.SAXException: Invalid element

    NULL 博文链接:https://wilian.iteye.com/blog/1992365

    新建Maven项目时出错:org.apache.maven.archiver.MavenArchiver.getManifest

    找到eclipse安装目录的features,将其目录中的org.sonatype.m2e.mavenarchiver.feature_0.NNN文件夹删除(也可能不存在),拷贝features/org.sonatype.m2e.mavenarchiver.feature_0.17.2.201609252051到features ...

    cvc-complex-type.2.4.d: Invalid content was found

    在XML Schema设计中,"cvc-complex-type.2.4.d: Invalid content was found" 是一个常见的错误信息,这通常意味着在解析XML文档时,遇到了不符合定义的复杂类型的内容。这个错误通常涉及到XML Schema的约束规则,即...

    invalid multibyte character sequence 870告警1

    Invalid Multibyte Character Sequence 警告解析 在编程中,特别是在嵌入式系统开发中,我们经常会遇到Invalid Multibyte Character Sequence 警告。这个警告通常来自于编译器,告知我们存在非法的多字节字符序列。...

    Caused by: android.system.ErrnoException: write failed: ENOSPC (

    ### 错误解析:Caused by: android.system.ErrnoException: write failed: ENOSPC (No space left 在Android开发过程中,可能会遇到“Caused by: android.system.ErrnoException: write failed: ENOSPC (No space ...

    org.springframework.web.servlet-3.0.1.RELEASE-A.jar

    Error creating bean with name 'org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]: Initialization of bean failed;...

    httpclient-4.5.12-API文档-中文版.zip

    Maven坐标:org.apache.httpcomponents:httpclient:4.5.12; 标签:apache、httpcomponents、httpclient、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档...

    org.springframework.transaction-3.1.2.RELEASE.zip

    nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'transactionManagerBeanName' of bean class [org.springframework.transaction.interceptor....

    发送邮件发送,先提一下SMTP

    ### 发送邮件的基础知识与实现方法 #### 一、SMTP简介 SMTP,即简单邮件传输协议(Simple Mail Transfer Protocol),是一种用于从源地址到目的地址传输邮件的标准协议。通过SMTP,我们可以控制邮件在网络中的传递...

    audience-annotations-0.5.0-API文档-中英对照版.zip

    Maven坐标:org.apache.yetus:audience-annotations:0.5.0; 标签:apache、yetus、audience、annotations、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览...

    rlexception: invalid roslaunch xml syntax: no element found: lin

    - **确保文件保存为UTF-8编码**:这是最常用的文本编码格式之一,可以有效避免因编码不兼容而导致的解析失败。 - 可以通过文本编辑器的保存选项设置编码方式。 #### 4. 检查空格和特殊字符 - **确保没有不必要的...

    Invalid name=”com.alibaba.dubbo.config.ProtocolConfig#0″

    java.lang.IllegalStateException: Invalid name=“com.alibaba.dubbo.config.ProtocolConfig#0” contains illegal character, only digit, letter, ‘-’, ‘_’ or ‘.’ is legal 原因: 如果没有指定id属性,...

    bcprov-jdk15on-1.46.jar中文文档.zip

    bcprov-jdk15on-***.jar中文文档.zip,java,bcprov-jdk15on-***.jar,org.bouncycastle,bcprov-jdk15on,***,jar包,Maven,第三方jar包,组件,开源组件,第三方组件,Gradle,bouncycastle,bcprov,jdk15on,中文API文档,手册,...

    dubbo-admin 管理后台2.5.4 for JDK8

    大家可以直接使用 Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'URIType' of bean class [com.alibaba.citrus.service.uribroker.uri.GenericURIBroker]: Bean property ...

    解决org.apache.ibatis.binding.BindingException: Invalid bound statement (not found

    出现以上的情况主要的原因是因为在主配置文件标签没正确的指向映射接口的配置文件。 解决方案:1.检查的name是否正确,如我的name属性填的就是com.it.dao 2、检查的class属性或resource属性,我resource这里写的是...

    Mybatis报错: org.apache.ibatis.exceptions.PersistenceException解决办法

    在使用Mybatis框架时,你可能会遇到`org.apache.ibatis.exceptions.PersistenceException`这样的异常。这个异常通常表示在执行数据库查询操作时遇到了问题。本篇将详细分析这个问题并提供解决方法。 ### 问题概述 ...

    httpcore-4.4.13-API文档-中文版.zip

    Maven坐标:org.apache.httpcomponents:httpcore:4.4.13; 标签:apache、httpcomponents、httpcore、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。...

    Nginx常见错误

    10. **`"upstream sent invalid header while reading response header from upstream"`**:后端服务器发送的响应头部无效。 11. **`"client intended to send too large body"`**:客户端发送的数据体超过了配置的...

    STM32f10x_startup

    STM32F10x系列是意法半导体(STMicroelectronics)推出的基于ARM Cortex-M3内核的微控制器,广泛应用于各种嵌入式系统设计。`STM32F10x_startup`指的是针对该系列芯片的启动文件,通常以`.s`格式存在,它是微控制器...

Global site tag (gtag.js) - Google Analytics