对于Dispaly.asyncExec方法与Display.syncExec
Dispaly.asyncExec(Runnable runnable)
一看名字就知道是一个执行异步运行线程的方法。但是有部分人会认为,如果在UI线程里调用的话会让UI线程异步开启一个线程,从而运行runnable。我不敢肯定正在阅读的您会这么认为,至少我在公司经常看到这种用法。其实,看一下API说明就知道了,“Causes the run() method of the runnable to be invoked by the user-interface thread at the next reasonable opportunity.The caller of this method continues to run in parallel, and is not notified when the runnable has completed.”,这句话就是说,调用者和runnable会并行运行,而且runnable会在UI线程里被执行。那什么时候使用才更好呢?或者说比较合适呢?当然是在非UI线程里面咯!!这个方法就是在非UI线程里面操作UI用的。否则直接在非UI线程里面操作UI的话会报一个叫IllegalThreadAcessException(希望我没记错,呵呵)。
对于Display.syncExec(Runnable runnable),就是和当前线程同步执行。用法和上面的差不多,但是要记得是同步哟!会占用当前线程的时间。