FORTUNE

          THE WAY TO THE MASTER...
          posts - 49, comments - 18, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          SWT和多線程結合使用的問題釋疑

          Posted on 2006-02-21 13:42 fortune 閱讀(748) 評論(0)  編輯  收藏 所屬分類: java技術

          SWT作為JAVA開源世界的優秀圖形庫,已經得到了很多java愛好者的青睞。我最近也在使用swt開發一些應用程序。我發現多線程中使用swt需要額外的技巧。

          情形:
              單擊一個按鈕,然后新開一個線程來執行一個復雜的任務。任務執行完時,彈出一個對話框提示任務執行完畢。

          示例1:

          package threadandui;

          import org.eclipse.swt.SWT;
          import org.eclipse.swt.widgets.Display;
          import org.eclipse.swt.widgets.MessageBox;
          import org.eclipse.swt.widgets.Shell;
          import org.eclipse.swt.widgets.Button;
          import org.eclipse.swt.events.SelectionAdapter;
          import org.eclipse.swt.events.SelectionEvent;

          public class TestSwt1 extends Shell {
                  public static Shell shell;
                  public static void main(String args[]) {
                          try {
                                  Display display = Display.getDefault();
                                  shell = new TestSwt1(display, SWT.SHELL_TRIM);
                                  shell.open();
                                  shell.layout();
                                  while (!shell.isDisposed()) {
                                          if (!display.readAndDispatch())
                                                  display.sleep();
                                  }
                          } catch (Exception e) {
                                  e.printStackTrace();
                          }
                  }

                  public TestSwt1(Display display, int style) {
                          super(display, style);
                          createContents();
                  }

                  protected void createContents() {
                          setText("SWT Application1");
                          setSize(500, 375);

                          final Button button = new Button(this, SWT.NONE);
                          button.addSelectionListener(new SelectionAdapter() {
                                  public void widgetSelected(SelectionEvent e) {
                                          new MyThread().start();
                                  }
                          });
                          button.setBounds(80, 50, 85, 25);
                          button.setText("start");
                  }

                  protected void checkSubclass() {
                  }
                  
                  class MyThread extends Thread{
                          public void run(){
                                  //complex task
          //                        for(int i=0;i < 10000;i++){
          //                                System.out.println(i);
          //                        }
                                  //display a dialog                         
                                  MessageBox mb = new MessageBox(shell);
                                  mb.setMessage("Task ended!");
                                  mb.open();
                          }
                  }
          }

          該程序在單擊start按鈕后會有異常:
          Exception in thread "Thread-0" org.eclipse.swt.SWTException: Invalid thread access
                  at org.eclipse.swt.SWT.error(SWT.java:2691)
                  at org.eclipse.swt.SWT.error(SWT.java:2616)
                  at org.eclipse.swt.SWT.error(SWT.java:2587)
                  at org.eclipse.swt.widgets.Widget.error(Widget.java:381)
                  at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:284)
                  at org.eclipse.swt.widgets.Dialog.checkParent(Dialog.java:154)
                  at org.eclipse.swt.widgets.Dialog.<init>(Dialog.java:116)
                  at org.eclipse.swt.widgets.MessageBox.<init>(MessageBox.java:81)
                  at org.eclipse.swt.widgets.MessageBox.<init>(MessageBox.java:54)
                  at threadandui.TestSwt1$MyThread.run(TestSwt1.java:70)

          為什么會有異常?沒有參考書,沒有人指導,你是就此罷休呢,還是刨根問底?我想知道答案,那我該怎么做呢?
          請注意,swt是開源的,代碼就是你最好的參考書,最好的指導老師!

          查看swt源代碼:at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:284)

          /*245*/        protected void checkWidget () {
                  
          /*246*/                Display display = this.display;
                  
          /*247*/                if (display == null) error (SWT.ERROR_WIDGET_DISPOSED);
                  
          /*248*/                if (display.thread != Thread.currentThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
                  
          /*249*/                if ((state & DISPOSED) != 0) error (SWT.ERROR_WIDGET_DISPOSED);

          /*250*/        }
          第248行有判斷“display.thread != Thread.currentThread ()”。顯然,在我們的示例中他們是不相等的。因為,MessageBox處在新創建的線程中,而display

          則處在main線程中。可見我們需要在新線程中創建一個display給MessageBox使用。“示例2”給出了答案。


          示例2;
          /*
          * Created on 2005-5-26
          *
          * TODO To change the template for this generated file go to
          * Window - Preferences - Java - Code Style - Code Templates
          */
          package threadandui;

          import org.eclipse.swt.SWT;
          import org.eclipse.swt.widgets.Display;
          import org.eclipse.swt.widgets.MessageBox;
          import org.eclipse.swt.widgets.Shell;
          import org.eclipse.swt.widgets.Button;
          import org.eclipse.swt.events.SelectionAdapter;
          import org.eclipse.swt.events.SelectionEvent;

          /**
          * @author jiangjunping
          *
          * TODO To change the template for this generated type comment go to
          * Window - Preferences - Java - Code Style - Code Templates
          */
          public class TestSwt2 extends Shell {
                  public static Shell shell;
                  public static void main(String args[]) {
                          try {
                                  Display display = Display.getDefault();
                                  shell = new TestSwt2(display, SWT.SHELL_TRIM);
                                  shell.open();
                                  shell.layout();
                                  while (!shell.isDisposed()) {
                                          if (!display.readAndDispatch())
                                                  display.sleep();
                                  }
                          } catch (Exception e) {
                                  e.printStackTrace();
                          }
                  }

                  public TestSwt2(Display display, int style) {
                          super(display, style);
                          createContents();
                  }

                  protected void createContents() {
                          setText("SWT Application2");
                          setSize(500, 375);

                          final Button button = new Button(this, SWT.NONE);
                          button.addSelectionListener(new SelectionAdapter() {
                                  public void widgetSelected(SelectionEvent e) {
                                          new MyThread().start();
                                  }
                          });
                          button.setBounds(80, 50, 85, 25);
                          button.setText("start");
                          //
                  }

                  protected void checkSubclass() {
                  }
                  
                  class MyThread extends Thread{
                          public void run(){
                                  //complex task
                                  for(int i=0;i < 50000;i++){
                                          System.out.println(i);
                                  }
                                  //display a dialog                         
                                  Display display = new Display();//new display
                                  Shell shell2 = new Shell(display);//added 
                                  MessageBox mb = new MessageBox(shell2);//use new display created in the current thread
                                  mb.setMessage("Task ended!");
                                  mb.open();
                                  display.dispose();//added
                          }
                  }
          }


          好了,程序正常了!

          主站蜘蛛池模板: 固始县| 保德县| 舒城县| 开封市| 麦盖提县| 灵台县| 襄垣县| 芒康县| 湘潭县| 商城县| 旺苍县| 万州区| 绥阳县| 凉山| 巩留县| 开化县| 阜阳市| 金溪县| 张家港市| 东海县| 唐山市| 元阳县| 黔江区| 江源县| 金塔县| 松阳县| 丘北县| 陈巴尔虎旗| 高邮市| 团风县| 乌鲁木齐县| 金堂县| 东乡| 大同市| 禹城市| 贵港市| 河北省| 朔州市| 张家口市| 祁门县| 新民市|