诸位童鞋们,我现在在写一个SWT的程序,其中有个功能是读取文件并进行解析,我的想法是用两个线程,一个读文件,一个更新UI,线程间通过Stack交换数据,可现在的问题一旦开始读文件,UI端就会死掉,一直要等文件解析完毕才能正常,加上Thread.sleep也没用。请问这个问题该怎么解决啊?有没有好的方案?
解决方案 »
- 求一个Java学习计划路线
- 请问一下这个小计算器的程序怎么运行不了
- 谁能告诉我,用java解析Excel时,环境怎么配置
- 什么样的java开发工具你认为最好,最有前途Borland Eclipse JDeveloper NetBeans IntelliJ Microsoft Visual J++
- 通过CompTIA Instructor+的最低分数是多少?
- object landscapes and lifetimes
- 不会安装JDK
- 编写一个简单程序
- 成立eclipse qq群276409欢迎加入
- java初学者疑惑,请各位高手指点!
- 各位大侠指导下小弟
- 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();
}
}}