java swt多线程问题的解决现场(一点一点耐心看)

   今天终于将一个头疼我好几天的问题解决了,难点 多线程、socket服务、swt中自己的线程模式;

   首先讲解一下我项目中遇到的问题,首先是我创建了一个shell窗口,做了一个内嵌浏览器,当我进入到index页面的时候,开始进行socket服务,我单独起的线程,负责接收客户端发送过来的消息,当我读到客户端发送的参数,我又创建一个线程去读取并返回给客户端,经过验证参数的正确性之后,控制内嵌浏览器的页面跳转,问题就出在这里,首先我以为是多线程之间通信的问题,于是我查了相关资料发现多线程之间相互调用是没有问题的。举例:

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(){

myt2.m4t2();//调用主线程的方法

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

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();  
     } 
}

子线程可以获取主线程的变量或者方法,这个流程是同的,我的线程模式跟着类似,于是我把我的代码开始移植到这里,进行进一步测试

 

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())

}
    });

果然没有问题,本来以为是线程通信的问题,经过不断的摸索和同事的帮助,非常感谢 

终于收获了这个问题,线程也学到了很多

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值