Display.getCurrent().asyncExec 不并行运行?

发布于 2024-11-28 18:41:58 字数 1194 浏览 1 评论 0原文

这是我的代码:

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);
                    }
                }
            }
        });

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还给你自由 2024-12-05 18:41:58

您正在 UI 线程上运行此数据库连接 Runnable - 这意味着您正在让 UI 线程无法处理任何其他消息,这些消息将导致它绘制、响应单击事件等。所以是的,当您运行时此数据库连接作业后,您的 UI 将无法执行任何其他操作,并且 UI 将变得无响应。

您可能不想希望在 UI 线程上运行此数据库连接作业,您可能希望在简单的后台线程中执行此操作,然后使用 显示#asyncExec

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.

谢绝鈎搭 2024-12-05 18:41:58

您可以使用 Eclipse Jobs API。

创建一个扩展 org.eclipse.core.runtime.jobs.Job 的类,将数据库代码粘贴到 run 方法中,然后调用 job.schedule()< /code> 安排并运行作业。

请访问 Lars Vogel 的网站了解更多示例。

You could use Eclipse Jobs API.

Create a class that extends org.eclipse.core.runtime.jobs.Job, stick your database code in the run method then call job.schedule() to schedule and run the job.

Have a look at Lars Vogel's site for a further example.

还不是爱你 2024-12-05 18:41:58

asyncExec 的要点是安排某些内容在 UI 线程中运行(并在安排后立即返回),因此它不能并行运行任何内容。

The point of asyncExec is to schedule something to run in the UI thread (and return immediately after scheduling), so it can't run anything in parallel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文