今天终于将一个头疼我好几天的问题解决了,难点 多线程、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())
}
});
果然没有问题,本来以为是线程通信的问题,经过不断的摸索和同事的帮助,非常感谢
终于收获了这个问题,线程也学到了很多