使用 WindowBuilder SWT 时 Java 中的多个线程

Multiple threads in Java while using WindowBuilder SWT

有人能解释一下为什么当我尝试 运行 2 个或更多线程时此程序中的 Window 冻结了吗?

我希望能够在两个线程都处于 运行ning 状态时单击 Hello 按钮。 这是我的简单项目。 任务 class 只打印出单词 "Testing".

public class Task extends Thread {
public static boolean keepRun = true;

public void run(){
    while(keepRun == true){
    System.out.println("Testing...");

    try {
        Thread.sleep(1000);
    }catch(InterruptedException e){}};
}
}

关闭 class 在控制台中键入 stop 时停止线程。

import java.util.Scanner;
public class Closing implements Runnable{
Scanner s = new Scanner(System.in);
public void run() {

    while (s.next().equals("stop")){
          System.out.println("Threads down");

          Task.keepRun =false;

    try {
        Thread.sleep(5000);
    }catch(InterruptedException e){} 
    };

}
}

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

这里是 Window class,main 也位于此处。

public class Window {

protected Shell shell;

/**
 * Launch the application.
 * @param args
 */
public static void main(String[] args) {
    try {
        Window window = new Window();
        window.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Open the window.
 */
public void open() {
    Display display = Display.getDefault();
    createContents();
    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
}

/**
 * Create contents of the window.
 */
protected void createContents() {
    shell = new Shell();
    shell.setSize(192, 208);
    shell.setText("SWT Application");

    Button btnRun = new Button(shell, SWT.NONE);
    btnRun.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            Task newTask = new Task();
            Closing closing = new Closing();
            newTask.start();
            closing.run();
        }
    });
    btnRun.setBounds(50, 32, 75, 25);
    btnRun.setText("Run");

    Button btnHello = new Button(shell, SWT.NONE);
    btnHello.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            System.out.println("Hello");
        }
    });
    btnHello.setBounds(50, 81, 75, 25);
    btnHello.setText("Hello");

}
}

重写:

Closing closing = new Closing();
newTask.start();
closing.run();

对此:

Closing closing = new Closing();
newTask.start();
new Thread(closing).start();

看这段代码:

public class Closing implements Runnable{
    Scanner s = new Scanner(System.in);
    public void run() {

        while (s.next().equals("stop")){
            System.out.println("Threads down");

            Task.keepRun =false;

            try {
                Thread.sleep(5000);
            }catch(InterruptedException e){} 
        };
    }
}

如果你简单地调用run();Thread.sleep(5000);会影响调用运行的线程,另一方面,当你创建一个新线程时,睡眠会影响到这个。