今天终于将一个头疼我好几天的问题解决了,难点多线程、socket服务、swt中自己的线程模式;
首先讲解一下我项目中遇到的问题,首先是我创建了一个shell窗口,做了一个内嵌浏览器,当我进入到index页面的时候,开始进行socket服务,我单独起的线程,负责接收客户端发送过来的消息,当我读到客户端发送的参数,我又创建一个线程去读取并返回给客户端,经过验证参数的正确性之后,控制内嵌浏览器的页面跳转,问题就出在这里,首先我以为是多线程之间通信的问题,于是我查了相关资料发现多线程之间相互调用是没有问题的。举例:
publicclassThread2{
publicvoidm4t1(){
synchronized(this){
inti=5;
while(i-->0){
System.out.println(Thread.currentThread().getName()+":"+i);
try{
Thread.sleep(500);
}catch(InterruptedExceptionie){
}
}
}
}
publicvoidm4t2(){
inti=5;
while(i-->0){
System.out.println(Thread.currentThread().getName()+":"+i);
try{
Thread.sleep(500);
}catch(InterruptedExceptionie){
}
}
}
publicstaticvoidmain(String[]args){
finalThread2myt2=newThread2();
finalThreadth=newThread(newRunnable(){
publicvoidrun(){
myt2.m4t2();//调用主线程的方法
//System.out.println(browser.getUrl());
System.out.println("-------");
}});
Threadt1=newThread(newRunnable(){th.start();},"t1");//线程1,开启子线程
Threadt2=newThread(newRunnable(){publicvoidrun(){myt2.m4t2();}},"t2");//线程2
t1.start();
t2.start();
}
}
子线程可以获取主线程的变量或者方法,这个流程是同的,我的线程模式跟着类似,于是我把我的代码开始移植到这里,进行进一步测试
publicclassThreadMainextendsShell{
publicstaticCoolBrowserbrowser;//封装的内嵌浏览器
publicstaticStringurlStr;//url地址
publicstaticCombourl;
publicThreadMain(Displaydisplay){
super(display,SWT.NO_TRIM);
createContents();//初始化窗口
}
publicvoidcreateContents(){
setText("SWTApplication");
setSize(1366,768);
setText("自助换电系统");
//CentreWnd(this);
Compositecontrols=newComposite(this,SWT.NONE);
browser=newCoolBrowser(this,SWT.NONE);
System.out.println(browser.handle);
url=newCombo(controls,SWT.ARROW_DOWN);
url.setFocus();
FormDatafd_controls=newFormData();
fd_controls.bottom=newFormAttachment(0,800);
fd_controls.right=newFormAttachment(0,1366);
controls.setLayoutData(fd_controls);
this.setLayout(newFormLayout());
StringprojPath=System.getProperty("user.dir");
browser.setSize(1366,768);
LogInfo.logInfo(NewCard.class,projPath+"====projPath");
urlStr=projPath+"/chargingStation/index.html";
browser.setUrl(urlStr);
browser.addLocationListener(newAdvancedLocationListener(url));
//以上是对浏览器和窗口的初始化
//browser.setUrl(urlStr);
}
publicvoidm4t1(){
synchronized(this){
inti=5;
while(i-->0){
System.out.println(Thread.currentThread().getName()+":"+i);
try{
Thread.sleep(500);
}catch(InterruptedExceptionie){
}
}
}
}
publicvoidm4t2(){
inti=5;
while(i-->0){
System.out.println(Thread.currentThread().getName()+":"+i);
try{
Thread.sleep(500);
}catch(InterruptedExceptionie){
}
}
}
@Override
protectedvoidcheckSubclass(){
//DisablethecheckthatpreventssubclassingofSWTcomponents
}
publicstaticvoidmain(String[]args){
Displaydisplay=Display.getDefault();
finalThreadMainmyt2=newThreadMain(display);
finalThreadth=newThread(newRunnable(){
publicvoidrun(){
myt2.m4t2();
System.out.println(browser.getUrl());//报错,获取当前的URL地址
System.out.println("-------");
}});
//finalThreadth =newThread(,"t3");
Threadt1=newThread(newRunnable(){publicvoidrun(){th.start();}},"t1");
Threadt2=newThread(newRunnable(){publicvoidrun(){myt2.m4t2();}},"t2");
t1.start();
t2.start();
}
}
启动报错org.eclipse.swt.SWTException:Subclassingnotallowed
重写checkSubclass()方法,方法体为空.即在子类中添加:
protectedvoidcheckSubclass()
{
}
重写之后报错org.eclipse.swt.SWTException:Invalidthreadaccess
System.out.println(browser.getUrl());//报错,获取当前的URL地址
于是我查了相关资料
在SWT程序中,
SWT会自动创建一个用户界面线程
非用户界面线程不能直接操作用户界面线程
于是我终于明白了,原来是这个东西在搞怪,
Display.getDefault().syncExec(newRunnable(){
publicvoidrun(){
System.out.println(browser.getUrl())
}
});
果然没有问题,本来以为是线程通信的问题,经过不断的摸索和同事的帮助,非常感谢
终于收获了这个问题,线程也学到了很多