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

时间:2015-07-12 13:10:04

标签: java multithreading swt

当我尝试运行2个或更多线程时,有人可以解释为什么这个程序中的Window会冻结吗?

我希望能够在两个线程都运行时单击Hello按钮。 这是我的简单项目。 任务类只打印出单词“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){}};
}
}

Closing类在控制台中键入时停止线程。

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类,其中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");

}
}

1 个答案:

答案 0 :(得分:1)

重写:

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);将受到调用run的线程的影响,另一方面,当你创建一个新线程时,睡眠将受此影响。