java swt多線程問題的解決現場(一點一點耐心看)

   今天終於將一個頭疼我好幾天的問題解決了,難點 多線程、socket服務、swt中本身的線程模式;html

   首先講解一下我項目中遇到的問題,首先是我建立了一個shell窗口,作了一個內嵌瀏覽器,當我進入到index頁面的時候,開始進行socket服務,我單獨起的線程,負責接收客戶端發送過來的消息,當我讀到客戶端發送的參數,我又建立一個線程去讀取並返回給客戶端,通過驗證參數的正確性以後,控制內嵌瀏覽器的頁面跳轉,問題就出在這裏,首先我覺得是多線程之間通訊的問題,因而我查了相關資料發現多線程之間相互調用是沒有問題的。舉例:shell

public class Thread2 {  
     public void m4t1() {  
          synchronized(this) {  
               int i = 5;  
               while( i-- > 0) {  
                    System.out.println(Thread.currentThread().getName() + " : " + i);  
                    try {  
                         Thread.sleep(500);  
                    } catch (InterruptedException ie) {  
                    }  
               }  
          }  
     }  
     public void m4t2() {  
          int i = 5;  
          while( i-- > 0) {  
               System.out.println(Thread.currentThread().getName() + " : " + i);  
               try {  
                    Thread.sleep(500);  
               } catch (InterruptedException ie) {  
               }  
          }  
     }  
     public static void main(String[] args) {  
          final Thread2 myt2 = new Thread2();  瀏覽器

     final Thread th= new Thread(new Runnable() {多線程

public void run(){ eclipse

myt2.m4t2();//調用主線程的方法socket

// System.out.println(browser.getUrl());ide

System.out.println("-------");測試

}});
             Thread t1 = new Thread(  new Runnable() {  th.start(); }, "t1"  );  //線程1,開啓子線程
          Thread t2 = new Thread(  new Runnable() {  public void run() { myt2.m4t2();   }  }, "t2"  );  //線程2
          t1.start();  
          t2.start();  
     } 
}this

子線程能夠獲取主線程的變量或者方法,這個流程是同的,個人線程模式跟着相似,因而我把個人代碼開始移植到這裏,進行進一步測試url

 

public class ThreadMain extends Shell {

public static CoolBrowser browser;//封裝的內嵌瀏覽器

public static  String urlStr;//url地址

public static Combo url;

public ThreadMain(Display display){

super(display, SWT.NO_TRIM);

createContents();//初始化窗口

}

public void createContents() {

setText("SWT Application");

setSize(1366, 768);

setText("自助換電系統");

//CentreWnd(this);

 Composite controls = new Composite(this, SWT.NONE);

 browser = new CoolBrowser(this, SWT.NONE);

 System.out.println(browser.handle);

 url = new Combo(controls, SWT.ARROW_DOWN);

 url.setFocus();

 FormData fd_controls = new FormData();

     fd_controls.bottom = new FormAttachment(0, 800);

     fd_controls.right = new FormAttachment(0, 1366);

     controls.setLayoutData(fd_controls);

 this.setLayout(new FormLayout());

     String projPath = System.getProperty("user.dir");

     browser.setSize(1366, 768);

     LogInfo.logInfo(NewCard.class,projPath+"====projPath");

     urlStr =projPath+"/chargingStation/index.html";

     browser.setUrl(urlStr);

     browser.addLocationListener(new AdvancedLocationListener(url));

   //以上是對瀏覽器和窗口的初始化    

      //  browser.setUrl(urlStr);

}

 public void m4t1() {  

         synchronized(this) {  

              int i = 5;  

              while( i-- > 0) {  

                   System.out.println(Thread.currentThread().getName() + " : " + i);  

                  

                   try {  

                        Thread.sleep(500);  

                   } catch (InterruptedException ie) {  

                   }  

              }  

         }  

    }  

    public void m4t2() {  

         int i = 5;  

         while( i-- > 0) {  

              System.out.println(Thread.currentThread().getName() + " : " + i);  

              try {  

                   Thread.sleep(500);  

              } catch (InterruptedException ie) {  

              }  

         }  

    }  

@Override

protected void checkSubclass() {

// Disable the check that prevents subclassing of SWT components

}

    public static void main(String[] args) {  

     Display display = Display.getDefault();

        final ThreadMain myt2 = new ThreadMain(display); 

        

       final Thread th= new Thread(new Runnable() {

public void run(){

myt2.m4t2();

 System.out.println(browser.getUrl());//報錯,獲取當前的URL地址

System.out.println("-------");

}});

          // final Thread th = new Thread(,"t3");

         Thread t1 = new Thread(  new Runnable() {  public void run() {th.start();  }  }, "t1"  );  

         Thread t2 = new Thread(  new Runnable() {  public void run() { myt2.m4t2();   }  }, "t2"  );  

         t1.start();  

         t2.start(); 

      

    } 

}

 

啓動報錯org.eclipse.swt.SWTException: Subclassing not allowed

重寫checkSubclass ()方法,方法體爲空.即在子類中添加: 

protected void checkSubclass()  

    {  

          

}  

重寫以後報錯 org.eclipse.swt.SWTException: Invalid thread access  

 

System.out.println(browser.getUrl());//報錯,獲取當前的URL地址

因而我查了相關資料

SWT程序中,

SWT會自動建立一個用戶界面線程

非用戶界面線程不能直接操做用戶界面線程

因而我終於明白了,原來是這個東西在搞怪,

 Display.getDefault().syncExec(new Runnable() {
    public void run() {
    System.out.println(browser.getUrl())

}
    });

果真沒有問題,原本覺得是線程通訊的問題,通過不斷的摸索和同事的幫助,很是感謝 

終於收穫了這個問題,線程也學到了不少