SWT多线程 诸位童鞋们,我现在在写一个SWT的程序,其中有个功能是读取文件并进行解析,我的想法是用两个线程,一个读文件,一个更新UI,线程间通过Stack交换数据,可现在的问题一旦开始读文件,UI端就会死掉,一直要等文件解析完毕才能正常,加上Thread.sleep也没用。请问这个问题该怎么解决啊?有没有好的方案? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给两个代码你 应该有些启发了package book.ch3;import org.eclipse.swt.SWT;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;public class UIThread_1 { public static void main(String args[]) { try { final Display display = Display.getDefault(); Shell shell = new Shell(display, SWT.SHELL_TRIM); shell.setText("Multi Thread"); shell.setSize(204, 92); final Button button = new Button(shell, SWT.NONE); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(final SelectionEvent e) { try { Thread.sleep(10000); button.setText("Execution done"); } catch (InterruptedException e1) { e1.printStackTrace(); } } }); button.setText("button"); button.setBounds(20, 15, 155, 25); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } catch (Exception e) { e.printStackTrace(); } }}package book.ch3;import org.eclipse.swt.SWT;import org.eclipse.swt.events.SelectionAdapter;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.widgets.Button;import org.eclipse.swt.widgets.Display;import org.eclipse.swt.widgets.Shell;public class UIThread_2 { public static void main(String args[]) { try { final Display display = Display.getDefault(); Shell shell = new Shell(display, SWT.SHELL_TRIM); shell.setText("Multi Thread"); shell.setSize(204, 92); final Button button = new Button(shell, SWT.NONE); button.addSelectionListener(new SelectionAdapter() { public void widgetSelected(final SelectionEvent e) { Thread thread = new Thread() { public void run() { try { Thread.sleep(10000); } catch (InterruptedException e1) { e1.printStackTrace(); } display.syncExec(new Runnable() { public void run() { button.setText("Execution Done"); } }); } }; thread.start(); } }); button.setText("button"); button.setBounds(20, 15, 155, 25); shell.open(); shell.layout(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } catch (Exception e) { e.printStackTrace(); } }} 给Stack加个锁,更新UI的那个一直循环,得到stack信息,然后更新到UI上。这样更新UI的线程等待,UI也不会死住 正则表达式问题,数字判断 强制类型装换是泛型的一种应用吗 问个计算机网络的初级问题 如何理解for (Iterator <Entry <UpdateVO, List <UpdateVO>>> 高分跪求利用POI提取WORD文档内容的方法!!!! 输出流的问题?急啊!!! 怎么把一个文件转化成字节流数组? 用Java中的什么方法使的一个程序实时性的运作 回答这个问题吧,你可以捡分了,呵呵 应怎样设CLASSPATH呢? 各位大侠指导下小弟 JPA OneToMany 映射问题
package book.ch3;import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;public class UIThread_1 { public static void main(String args[]) {
try {
final Display display = Display.getDefault();
Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setText("Multi Thread");
shell.setSize(204, 92);
final Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
try {
Thread.sleep(10000);
button.setText("Execution done");
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
});
button.setText("button");
button.setBounds(20, 15, 155, 25); shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package book.ch3;import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;public class UIThread_2 { public static void main(String args[]) {
try {
final Display display = Display.getDefault();
Shell shell = new Shell(display, SWT.SHELL_TRIM);
shell.setText("Multi Thread");
shell.setSize(204, 92);
final Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e) {
Thread thread = new Thread() {
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
display.syncExec(new Runnable() {
public void run() {
button.setText("Execution Done");
}
});
}
};
thread.start();
}
});
button.setText("button");
button.setBounds(20, 15, 155, 25); shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
} catch (Exception e) {
e.printStackTrace();
}
}}