前天把 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
?