2010-03-03 48 views
3

我第一次使用线程时遇到了一个问题,在主线程中的SWT程序中,我创建了GUI并打开了shell,然后启动了一个新线程以运行一些逻辑模型,并且在模型中处于某种特定状态时,在GUI类中调用了一个方法...这里是问题 在第二个线程中调用此方法,而我希望在主线程中调用此方法或至少在主线程中执行它SWT单线程问题

我该如何解决这个问题? 谢谢,

+0

相关链接:http://help.eclipse.org/help32/ http://stackoverflow.com/questions/1333377/how-to-update-a-gui-from-another-thread-in-java – 2010-03-03 23:29:22

回答

3

外部线程无法访问GUI。检查display.asyncExec

+0

http://help.eclipse.org/help32/ index.jsp?topic =/org.eclipse.platform.doc.isv/guide/swt_threading.htm – 2010-03-03 23:22:45

+2

http://book.javanb.com/swt-the-standard-widget-toolkit/ch05lev1sec7.html – 2010-03-03 23:23:04

3

您需要使用在DisplayasyncExecsyncExec方法,以便在主线程中执行的可运行:

// do stuff in a background thread 

// ...then schedule job to run in main thread 
display.asyncExec(new Runnable() { 
    ... 
}); 

两个syncExecasyncExec将在主(UI)线程调度作业尽快地。不同的是,asyncExec立即返回,而syncExec将不会返回,直到作业完成。