58,454
社区成员
发帖
与我相关
我的任务
分享
Button bt = new Button ( parent , SWT.NONE);
bt.setText("开始一个任务");
text1=new Text(parent, 0);
bt.addSelectionListener( new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
run_1 r=new run_1();
r.run();//run方法就是包含死循环的那个方法
x=String.valueOf(r.q);
System.out.println("listen "+x);
text1.setText("run_1线程名:"+Thread.currentThread().getName()+" 线程值:"+x);
}
});
while(true) {
if(flag) {
doSomthing();
} else {
continue;
}
}
public static void main (String [] args) {
final int[] i = new int[0];
Display display = new Display ();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout(SWT.VERTICAL));
final Text text = new Text(shell, SWT.NONE);
Button button = new Button(shell, SWT.NONE);
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// 线程的声明。
Thread thread = new Thread() {
@Override
public void run() {
// 变量+1
i[0]++;
// 下面异步更新界面,重设Text值。
text.getShell().getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
text.setText(String.valueOf(i[0]));
}
});
}
};
// 启动线程。
thread.start();
}
});
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
bt.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e)
r.run();
rft= new refreshText(r,text1);
rft.run();
}
});
public refreshText(run_1 r,Text t){
String y = String.valueOf(r.q);
t.setText("线程名:" + Thread.currentThread().getName() + " 线程值:" + y);
}
public void run() {
Thread t1 = new Thread();
t1.start();
}
private boolean stop = true;
public void run() {
while(true){
System.out.println("Thread is running!");
}
}
public void exit(){
this.stop=false;
System.out.println("Thread stop!");
}
bt.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Thread thread = new Thread() {
public void run() {
final Thread th = new Thread(r, "run_1");
final Thread th2 = new Thread(r2, "run_2");
th.start();
th2.start();
while (r.q != 0 || r2.q != 0) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
text1.setText(th.getName()+"线程值: " + r.q);
text2.setText(th2.getName()+"线程值: " + r2.q);
}});}}};
thread.start();}});
while(1==1){
r.run();
x=r.q;
System.out.println("x= "+x);
text1.setText(x);
}