SWT中多线程的处理
来源:互联网 发布:js confirm的用法 编辑:程序博客网 时间:2024/05/02 12:49
SWT中处理多线程
在Eclipse中,SWT是最重要的基石之一,负责了跨平台的本地界面显示,使Java也能够编写与系统相同的界面元素。在SWT中处理多线程也是其重要技术的表现。
SWT有两个使用方法,分别是asyncExec和syncExec,前者是 异步执行与界面有关的操作,后者反之。为什么要这么做?由于SWT单独有一个线程(主线程)处理界面显示,数据显示等,如果要在其他线程中操作界面元素, 就必须使用上述两个方法执行,即将另外线程的操作交给主线程处理。
而SWT的多线程远没有这么简单。一般情况下,在SWT中运行线程时,其主线程不能阻塞,也能响应用户请求,比如鼠标和菜单等。在这种情况下,需要新建Thread来处理逻辑,在这个Thread中,必须使用上面的两个方法处理界面数据。
以下是一个简单的例子,启动10个线程,在5秒的时间内,刷新列表中各行的字符串,在这个过程中,主界面依然能够响应鼠标请求。代码有点“圆环套圆环”的感觉。
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
/**
* @author tenyears.cn
*/
public class SWTThread {
private int size = 10; // 10 threads
private long runTime = 5000; // 5 seconds
private List list;
private Shell shell;
public void startThread() {
for (int index = 0; index < size; index++) {
final int listIndex = index;
final Runnable refresh = new Runnable() {
public void run() {
final Display disp = shell.getDisplay();
final Runnable runOne = new Runnable() {
public void run() {
final long start = System.currentTimeMillis();
while ((System.currentTimeMillis() - start) < runTime) {
disp.syncExec(new Runnable() {
public void run() {
if (list.isDisposed())
return;
String string = "No." + listIndex;
string += " " + (System.currentTimeMillis() - start);
list.setItem(listIndex, string);
}
});
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
};
}
};
Thread thread = new Thread(runOne);
thread.start();
}
};
BusyIndicator.showWhile(shell.getDisplay(), refresh);//这一句很关键
}
}
public Shell open(final Display display) {
shell = new Shell(display, SWT.DIALOG_TRIM | SWT.MIN);
shell.setText("SWT Thread Test");
shell.setLayout(new GridLayout(1, true));
list = new List(shell, SWT.BORDER);
list.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
for (int index = 0; index < size; index++)
list.add("String " + (index + 1));
Button startBtn = new Button(shell, SWT.PUSH);
startBtn.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
startBtn.setText("Start");
SelectionAdapter selection = new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
startThread();
}
};
startBtn.addSelectionListener(selection);
shell.setSize(400, 300);
shell.open();
return shell;
}
public static void main(String[] args) {
Display display = new Display();
SWTThread application = new SWTThread();
Shell shell = application.open(display);
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
说明:
下面分析startThread()
这个方法可以用于当这个方法的执行需要很长时间,而在其执行期间又不会影响其他方法的执行。本方法中的最后一句:
BusyIndicator.showWhile(shell.getDisplay(), refresh);
是关键,如果没有这一句则,执行此方法时,其他方法不能运行。
这个方法的必须包含两个Runnable的是实现:其中外层用于
BusyIndicator.showWhile()方法中作为参数
长时间的运行代码应该放在里面的一个runnable实现的run()方法中,例如:
final Runnable runOne = new Runnable() {
public void run() {
final long start = System.currentTimeMillis();
while ((System.currentTimeMillis() - start) < 1000*10) {
cnt++;
}
};
};
startThread方法可以简化为:(这个方法更清晰)
public void startThread() {
final Runnable refresh = new Runnable() {
public void run() {
final Runnable runOne = new Runnable() {
public void run() {
final long start = System.currentTimeMillis();
while ((System.currentTimeMillis() - start) < 1000*10) {
cnt++;
}
};
};
Thread thread = new Thread(runOne);
thread.start();
}
};
BusyIndicator.showWhile(shell.getDisplay(), refresh);
}
- SWT中多线程的处理
- SWT中处理多线程
- SWT中处理多线程
- swt 多线程注意的问题
- 从一个简单的例子看UI多线程处理(基于eclipse的SWT组件)
- Qt 中多线程的处理
- java 中处理多线程的
- SWT 多线程
- eclipse中swt的起步
- SWT中TableViewer的使用:
- SWT中Button的用法
- SWT中按钮的监听器
- SWT中处理组件的内容变更事件(类似onchange),ModifyListener的使用
- 学习SWT的笔记 --键盘事件处理
- SWT事件处理的常用写法
- SWT/JFACE Viewer的cellEditor处理流程
- MFC中多线程的退出处理
- wxPython中多线程处理的研究
- 四甲基乙二胺 tetramethylethylenediamine
- 重启网卡
- VSS实例练习
- tetramethylethylenediamine 四甲基乙二胺
- (转贴)错误"Lc.exe 已退出,代码 -1 "
- SWT中多线程的处理
- 如何预防颈椎病?
- java初学者-垃圾收集器
- vmware虚拟机上的linux加载u盘的方法
- 单元测试工具试用
- Tetramethylethylenediamine
- 工程进度
- 朋友们的生日...
- c# linklabel 去掉下划线的方法