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 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在 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.您可以使用 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 therun
method then calljob.schedule()
to schedule and run the job.Have a look at Lars Vogel's site for a further example.
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.