您的位置:首页 > 其它

SWT(JFace)体验之多线程问题

2009-06-23 15:55 351 查看
测试代码如下:

SyncExecExample.java

package swt_jface.demo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SyncExecExample {
public static void main(String[] args) {
final Display display = new Display();
Shell shell = new Shell(display);

final Runnable print = new Runnable() {
public void run() {
System.out.println("Print from thread: /t" + Thread.currentThread().getName());
}
};

final Thread applicationThread = new Thread("applicationThread") {
public void run() {
System.out.println("Hello from thread: /t" + Thread.currentThread().getName());
display.syncExec(print);
System.out.println("Bye from thread: /t" + Thread.currentThread().getName());
}
};

shell.setText("syncExec Example");
shell.setSize(300, 100);

Button button = new Button(shell, SWT.CENTER);
button.setText("Click to start");
button.setBounds(shell.getClientArea());
button.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
applicationThread.start();
}
});
shell.open();

while(! shell.isDisposed()) {
if(! display.readAndDispatch()) {
display.sleep();
}
}

display.dispose();

}
}


再来一个例子:

PICalculator.java

package swt_jface.demo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class PICalculator {

Display display = new Display();
Shell shell = new Shell(display);
Button buttonThread = new Button(shell, SWT.PUSH);
Button buttonAsyncExec = new Button(shell, SWT.PUSH);
public PICalculator() {
shell.setText("PI Calculator");
shell.setSize(400, 80);
Rectangle clientArea = shell.getClientArea();
buttonThread.setText("Click here to calculate PI  [Non-UI thread UI Update]");
buttonThread.setBounds(
clientArea.x,
clientArea.y,
clientArea.width,
clientArea.height / 2);
buttonThread.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
buttonThread.setText("Calculation in progress ...");
getTask(buttonThread).start();
}
});

buttonAsyncExec.setText("Click here to calculate PI  [asynExec method UI Update]");
buttonAsyncExec.setBounds(
clientArea.x,
clientArea.y + clientArea.height / 2,
clientArea.width,
clientArea.height / 2);
buttonAsyncExec.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
buttonAsyncExec.setText("Calculation in progress ...");
getTask2(buttonAsyncExec).start();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public Thread getTask(Button button) {
final Button theButton = button;
return new Thread() {
public void run() {
double pi = calculatePI(9999999);
theButton.setText("PI = " + pi);
}
};
}
public Thread getTask2(Button button) {
final Button theButton = button;
return new Thread() {
public void run() {
final double pi = calculatePI(9999999);
display.asyncExec(new Runnable() {
public void run() {
theButton.setText("PI = " + pi);
}
});
}
};
}
public static double calculatePI(int nestedLevel) {
double product = 1;
double lastSqrtValue = 0;
for (int i = 0; i < nestedLevel; i++) {
double sqrt = getNextSqrtValue(lastSqrtValue);
product *= 0.5 * sqrt;
lastSqrtValue = sqrt;
}
return 2 / product;
}
public static double getNextSqrtValue(double lastValue) {
return Math.sqrt(2 + lastValue);
}

public static void main(String[] args) {
new PICalculator();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: