java asyncexec_java – 等待所有提交到SWT UI线程的Runnables与Display :: asyncExec()完成

org.

eclipse.swt.widgets.Display.readAndDispatch()将处理事件队列中的事件,如果没有其他事件要处理,则返回false.但您可能不希望在处理事件时使用它.

asyncExec(*)是一个FIFO队列(虽然OS图形事件取代了asyncExecs),因此您可以执行大部分长时间运行的op处理,然后在队列中放置最终的asyncExec:

final boolean[] done = new boolean[1];

Runnable r = new Runnable() {

public void run() {

done[0] = true;

}

};

// now wait for the event somehow. The brute force method:

while (!done[0]) {

Thread.sleep(200);

}

理论上,从你长时间运行的op中产生的所有其他asyncExecs将在你到达最后一个时完成.

编辑:潜在的其他选择

创建自己的org.eclipse.core.runtime.jobs.Job,然后在最后加入()它:

public static class RefCountJob extends Job {

public RefCountJob() {

super("REF_COUNT");

}

int count = 0;

public void increment() {

count++;

}

public void decrement() {

count--;

}

@Override

protected IStatus run(IProgressMonitor monitor) {

monitor.beginTask("WAITING", IProgressMonitor.UNKNOWN);

while (count > 0) {

Thread.sleep(200);

monitor.worked(1);

}

monitor.done();

return Status.OK_STATUS;

}

}

要使用它,每次你要发射事件时递增()它,并在它们完成时让它们减少它(你必须确保它们减少它而不管抛出什么异常:-)

RefCountJob ref = new RefCountJob();

// ... do stuff, everybody increments and decrements ref

ref.increment();

// ... do more stuff

ref.increment();

// at the end of your long-running job

ref.schedule();

ref.join();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值