Display.getCurrent()。asyncExec是否不能并行运行? [英] Display.getCurrent().asyncExec not run in parallel?

查看:79
本文介绍了Display.getCurrent()。asyncExec是否不能并行运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

  Display.getCurrent()。asyncExec(new Runnable( ){
public void run(){
try {
Event e1 = new Event();
e1.type = EVT_CONNECTING;
for(Listener listener:listeners ){
listener.handleEvent(e1);
}
database = new Database(cp.getName(),cp.getConnection());
Event e2 = new Event( );
e2.type = EVT_CONNECT_SUCCESS;
for(侦听器监听器:监听器){
listener.handleEvent(e2);
}
} catch(异常例外) {
log.error(ex.getMessage(),ex);
事件e =新Event();
e.text = ex.getMessage();
e.type = EVT_CONNECT_FAILD;
for(Listener listener:listeners){
listener.handleEvent(e);
}
}
}
});

在上面的代码中,我尝试连接到数据库。有时这会花很长时间才能给出响应(例如,网络连接超时),但是当 Runnable 开始运行时,用户界面会丢失响应。为什么?

解决方案

您正在UI线程上运行此数据库连接Runnable-这意味着您正在饿死UI线程处理任何其他可能导致其绘制,响应单击事件等的消息。因此,是的,当您运行此数据库连接作业时,您的UI将无法执行其他任何操作,并且UI将变得无响应。 / p>

您可能想要在UI线程上运行此数据库连接作业,您可能希望在简单的后台线程中执行此操作,并且然后,在数据库连接作业完成后,使用 Display#asyncExec 将结果发布回UI线程。


Here is my code:

Display.getCurrent().asyncExec(new Runnable() {
            public void run() {
                try {
                    Event e1 = new Event();
                    e1.type = EVT_CONNECTING;
                    for (Listener listener : listeners) {
                        listener.handleEvent(e1);
                    }
                    database = new Database(cp.getName(), cp.getConnection());
                    Event e2 = new Event();
                    e2.type = EVT_CONNECT_SUCCESS;
                    for (Listener listener : listeners) {
                        listener.handleEvent(e2);
                    }
                } catch (Exception ex) {
                    log.error(ex.getMessage(), ex);
                    Event e = new Event();
                    e.text = ex.getMessage();
                    e.type = EVT_CONNECT_FAILD;
                    for (Listener listener : listeners) {
                        listener.handleEvent(e);
                    }
                }
            }
        });

In above code, I try to connect to a database. Sometimes this will take a long while to give response (network connection timeout for example), but when the Runnable begin to run, the user interface lose response. Why?

解决方案

You're running this database connection Runnable on the UI thread - that means that you're starving the UI thread of processing any other messages that would cause it to paint, respond to click events, etc. So yes, while you're running this database connection job, your UI will not be able to do anything else and the UI will become unresponsive.

You probably do not want to run this database connection job on the UI thread, you probably want to do it in a simple background thread, and then post the results back up to the UI thread by using Display#asyncExec once the database connection job has finished.

这篇关于Display.getCurrent()。asyncExec是否不能并行运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆