Java 时间密集型计算和SWT
在Java 时间密集型计算和SWT,java,asynchronous,swt,Java,Asynchronous,Swt,在Swing中,我创建SwingWorkers或使用invokeLater进行时间密集型计算,而不会干扰Swings GUI线程。在SWT中如何做到这一点?我正在使用Callable和Future编写代码,但我认为这不会减少它: class MyClass extends ViewPart { . . . @Override public void createPartControl(final Composite arg0) { th
Swing
中,我创建SwingWorkers
或使用invokeLater
进行时间密集型计算,而不会干扰Swings GUI线程。在SWT中如何做到这一点?我正在使用Callable和Future编写代码,但我认为这不会减少它:
class MyClass extends ViewPart {
.
.
.
@Override
public void createPartControl(final Composite arg0) {
this.runScenarioItem.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent e) {
final ScenarioDialog scenarioDialog = new ScenarioDialog(arg0.getShell(), SWT.DIALOG_TRIM
| SWT.APPLICATION_MODAL);
final Scenario scenario = scenarioDialog.open();
if (suvConnection.isConnected()) {
runScenarioItem.setEnabled(false);
try {
final ScenarioRunner runner = new ScenarioRunner(suvConnection, scenario);
final ExecutorService executor = new ScheduledThreadPoolExecutor(1);
final Future<Integer> future = executor.submit(runner);
System.out.println("result of callable = " + future.get());
runScenarioItem.setEnabled(true);
}
catch (final Exception e1) {
e1.printStackTrace();
}
}
}
});
}
}
当我有更多信息时,我会更新。伙计,我想念Swing…你想在屏幕上调用
asyncExec
。请阅读文件了解更多详情
下面是该文档中的相关示例代码片段,它显示了如何使用它重新绘制窗口:
// do time-intensive computations
...
// now update the UI. We don't depend on the result,
// so use async.
display.asyncExec (new Runnable () {
public void run () {
if (!myWindow.isDisposed())
myWindow.redraw ();
}
});
// now do more computations
...
首先,在你的问题中,你应该避免主观印象,它们无助于回答问题,其次,对于如何从另一个线程访问小部件的片段,请参见,这是必要的,如果您使用SWT结合Eclipse RCP框架,您应该考虑在./p>中处理长时间运行的进程。我看到了这些文档,但我仍然不明白。你是说我更新了我的数字运算代码里面的显示?SWT实际上是垃圾桶。Display.asyncExec大致相当于SwingUtilities.invokeLater。你把Runnables传给两个人。两者都将采用可运行的,然后在主UI线程上调度它。由于两者都是异步的,所以UI更新不会立即发生,但在可以这样做的时候,可以发布一个工作片段吗?运行此代码不会使UI线程饥饿吗?谢谢Tom。对不起,你的咆哮毫无帮助:)请将相关部分复制到你的答案中,因为网上所有的网站都来去匆匆。
// do time-intensive computations
...
// now update the UI. We don't depend on the result,
// so use async.
display.asyncExec (new Runnable () {
public void run () {
if (!myWindow.isDisposed())
myWindow.redraw ();
}
});
// now do more computations
...