2006-9-12

前天把 SWT 的多线程终于搞定,本来想写点心得,苦于没时间,今天刚好有时间,就写了如下拙作, ^_^

 

1 .先看看 JAVA JDK 里的多线程:

JDK 里的多项程里有两种方式: extends Thread implements Runnable ,当调用线程类的 start() 方法,本质是调用 run() ,线程执行。

e.g.

package com.javathread;

 

public class TestThread {

 

       /**

        * @param args

        */

       public static void main(String[] args) {

              // TODO Auto-generated method stub

              Thread t1 = new NewThread();// Create two new Thread

              t1.setName("t1");

              Thread t2 = new NewThread();

              t2.setName("t2");

              // new NewThread().start();//call the newThread

              try {

                     for (int i = 5; i > 0; i--) {

                            System.out.println("main Thread " + i);

                            t1.start();

 

                            t2.start();

                            Thread.sleep(1000);

 

                     }

              } catch (InterruptedException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

 

              System.out.println("Exiting main Thread!");

 

       }

 

}

 

/*

 * create a Thread class

 */

class NewThread extends Thread {

 

       public void run() {

 

              System.out.println("new  Thread " + this.getName());

       }

 

} 输出结果:

main Thread 5

new  Thread t1

new  Thread t2

main Thread 4

main Thread 3

main Thread 2

main Thread 1

Exiting main Thread!

 

Implements Runnable()

package com.javathread;

 

public class TestThread {

 

       /**

        * @param args

        */

       public static void main(String[] args) {

              // TODO Auto-generated method stub

              new NewThread("one");// Create two new Thread

 

              new NewThread("two");

 

              // new NewThread().start();//call the newThread

              try {

 

                     Thread.sleep(10000);

 

              } catch (InterruptedException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

 

              System.out.println("Exiting main Thread!");

 

       }

 

}

 

/*

 * create a Thread class

 */

class NewThread implements Runnable {

       String name;

 

       Thread t;

 

       NewThread(String name) {

              this.name = name;

              t = new Thread(this, name);

              System.out.println("New Thread " + t);

              t.start();

 

       }

 

       public void run() {

              try {

                     for (int i = 5; i > 0; i--) {

                            System.out.println(name + ":" + i);

                            Thread.sleep(1000);

                     }

              } catch (InterruptedException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

 

       }

 

}

 

但是 SWT 的线程和 JDK 的有点区别

 

当启动一个 SWT 的线程时候,要回过来操作 shell 上的控件

必须要调用 Display SyncExec 同步或者 asyncExec 异步,来重新取得 shell

举个例子

如下:

 

我们新建 SWT Application ,加一个 button 启动另一个线程,

 

代码如下所示:

 

package com;

 

import org.eclipse.jface.dialogs.MessageDialog;

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.SelectionAdapter;

import org.eclipse.swt.events.SelectionEvent;

import org.eclipse.swt.widgets.Button;

import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Shell;

 

public class Test {

 

       private static Display display = Display.getDefault();

 

       private static Shell shell = new Shell();

 

       public static void main(String[] args) {

              Test test = new Test();

              test.open();

 

              // final Shell shell = new Shell();

 

       }

 

       public void open() {

              shell.setSize(500, 375);

              shell.setText("SWT Application");

              shell.open();

 

              final Button startnewthreadButton = new Button(shell, SWT.NONE);

              startnewthreadButton.addSelectionListener(new SelectionAdapter() {

                     public void widgetSelected(SelectionEvent e) {

                            new TestThread().start();

 

                     }

              });

              startnewthreadButton.setText("startnewthread");

              startnewthreadButton.setBounds(135, 116, 90, 25);

              shell.layout();

              while (!shell.isDisposed()) {

                     if (!display.readAndDispatch())

                            display.sleep();

              }

       }

 

       /*

        * create a new Thread class

        */

       private class TestThread extends Thread {

              public void run() {

                     /*

                      * call Swt controls must use Dispaly.syncExec or Display.asyncExec

                      */

 

                     if (!Test.display.isDisposed()) {

                            Test.display.syncExec(new Runnable() {// 这段代码很重要

 

                                   public void run() {

 

                                          MessageDialog.openInformation(null, "fd", "fd");

 

                                   }

 

                            });

                     }

 

              }

       }

 

}

 

如果不加加红这段代码 直接 MessageDialog.openInformation(null,”fd”,”fd”);

程序会抛出异常: ja va.lang.ExceptionInInitializerError

 

好了 有问题 联系 edsionchen002@163.com

QQ 4384919