SWT中的线程同步_Display.syncExec(Runnable)

不少人遇到过SWT中画面阻塞的问题,下面是一个解决方法,可供参照: import org.eclipse.swt.SWT; import org.eclipse.swt.events.DisposeEvent; import org.eclipse.swt.events.DisposeListener; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ThreadTest { private Display display = null; private Shell shell = null; private int width = 600; private int height = 450; private Control movable = null; private Button action = null; private Thread thread = null; private boolean threadState = true; private static final Object LOCK = new Object(); public ThreadTest() { display = Display.getDefault(); shell = new Shell(display, SWT.SHELL_TRIM); shell.setSize(width, height); shell.setText("Thread Test"); shell.addDisposeListener(new DisposeListener() { public void widgetDisposed(DisposeEvent arg0) { thread.interrupt(); System.exit(0); } }); Rectangle monitorBounds = display.getPrimaryMonitor().getBounds(); shell.setLocation((monitorBounds.width - width) / 2, (monitorBounds.height - height) / 2); initMovableComposite(); initThread(); initActionButton(); shell.layout(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void initThread() { thread = new Thread() { Runnable runnable = new Runnable() { int deltX, deltY; Rectangle rect; public void run() { rect = movable.getBounds(); if (rect.x + rect.width >= shell.getBounds().width) { deltX = -1; } else if (rect.x <= 0) { deltX = 1; } if (rect.y + rect.width >= shell.getBounds().height) { deltY = -1; } else if (rect.y <= 0) { deltY = 1; } movable.setLocation(rect.x + deltX, rect.y + deltY); } }; public void run() { while (true) { synchronized (LOCK) { if (threadState) { display.syncExec(runnable); try { sleep(8); } catch (InterruptedException e) { e.printStackTrace(); } } else { try { sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } } } }; } private void initMovableComposite() { movable = new Button(shell, SWT.TRANSPARENCY_ALPHA); ((Button)movable).setText("O"); movable.setBounds(0, 0, 80, 24); } private void initActionButton() { action = new Button(shell, SWT.ARROW | SWT.DOWN); action.setText("Act"); int w = 80; int h = 25; action.setBounds((width - w) / 2, h % 10, w, h); action.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent event) { if (!thread.isAlive()) { thread.start(); } else { threadState = !threadState; } } public void widgetDefaultSelected(SelectionEvent event) {} }); } public static void main(String[] args) { new ThreadTest(); } }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值