SWT中多线程的处理

来源:互联网 发布:js confirm的用法 编辑:程序博客网 时间:2024/05/02 12:49

SWT中处理多线程

Eclipse中,SWT是最重要的基石之一,负责了跨平台的本地界面显示,使Java也能够编写与系统相同的界面元素。在SWT中处理多线程也是其重要技术的表现。

SWT有两个使用方法,分别是asyncExecsyncExec,前者是 异步执行与界面有关的操作,后者反之。为什么要这么做?由于SWT单独有一个线程(主线程)处理界面显示,数据显示等,如果要在其他线程中操作界面元素, 就必须使用上述两个方法执行,即将另外线程的操作交给主线程处理。

SWT的多线程远没有这么简单。一般情况下,在SWT中运行线程时,其主线程不能阻塞,也能响应用户请求,比如鼠标和菜单等。在这种情况下,需要新建Thread来处理逻辑,在这个Thread中,必须使用上面的两个方法处理界面数据。

以下是一个简单的例子,启动10个线程,在5秒的时间内,刷新列表中各行的字符串,在这个过程中,主界面依然能够响应鼠标请求。代码有点圆环套圆环的感觉。

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

/**
 @author tenyears.cn
 */
public class SWTThread {
  private int size = 10// 10 threads
  private long runTime = 5000// 5 seconds
  private List list;
  private Shell shell;
  public void startThread() {
    for (int index = 0; index < size; index++) {
      final int listIndex = index;
      final Runnable refresh = new Runnable() {
        public void run() {
          final Display disp = shell.getDisplay();
          final Runnable runOne = new Runnable() {
            public void run() {
              final long start = System.currentTimeMillis();
              while ((System.currentTimeMillis() - start) < runTime) {
                disp.syncExec(new Runnable() {
                  public void run() {
                    if (list.isDisposed())
                      return;
                    String string = "No." + listIndex;
                    string += " " + (System.currentTimeMillis() - start);
                    list.setItem(listIndex, string);
                  }
                });
                try {
                  Thread.sleep(100);
                catch (InterruptedException e) {
                }
              };
            }
          };
          Thread thread = new Thread(runOne);
          thread.start();
        }
      };
      BusyIndicator.showWhile(shell.getDisplay(), refresh);//
这一句很关键
    }
  }

  public Shell open(final Display display) {
    shell = new Shell(display, SWT.DIALOG_TRIM | SWT.MIN);
    shell.setText("SWT Thread Test");
    shell.setLayout(new GridLayout(1true));
    list = new List(shell, SWT.BORDER);
    list.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    for (int index = 0; index < size; index++)
      list.add("String " + (index + 1));
    Button startBtn = new Button(shell, SWT.PUSH);
    startBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    startBtn.setText("Start");
    SelectionAdapter selection = new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        startThread();
      }
    };
    startBtn.addSelectionListener(selection);
    shell.setSize(400300);
    shell.open();
    return shell;
  }

  public static void main(String[] args) {
    Display display = new Display();
    SWTThread application = new SWTThread();
    Shell shell = application.open(display);
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}

说明:

下面分析startThread()

这个方法可以用于当这个方法的执行需要很长时间,而在其执行期间又不会影响其他方法的执行。本方法中的最后一句:

BusyIndicator.showWhile(shell.getDisplay(), refresh);

是关键,如果没有这一句则,执行此方法时,其他方法不能运行。

这个方法的必须包含两个Runnable的是实现:其中外层用于

BusyIndicator.showWhile()方法中作为参数

长时间的运行代码应该放在里面的一个runnable实现的run()方法中,例如:

 

final Runnable runOne = new Runnable() {

        public void run() {

        final long start = System.currentTimeMillis();

         while ((System.currentTimeMillis() - start) < 1000*10) {

                       cnt++;

                  }            

               };

       };      

 

startThread方法可以简化为:(这个方法更清晰)

   

public void startThread() {

      

          final Runnable refresh = new Runnable() {

            public void run() {

             

              final Runnable runOne = new Runnable() {

                public void run() {

                  final long start = System.currentTimeMillis();

                  while ((System.currentTimeMillis() - start) < 1000*10) {

                        cnt++;

                   }            

                  };

                };            

              Thread thread = new Thread(runOne);

              thread.start();

            }

          };

          BusyIndicator.showWhile(shell.getDisplay(), refresh);

        }

 

 

原创粉丝点击