Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用WindowBuilder SWT时Java中的多线程_Java_Multithreading_Swt - Fatal编程技术网

使用WindowBuilder SWT时Java中的多线程

使用WindowBuilder SWT时Java中的多线程,java,multithreading,swt,Java,Multithreading,Swt,有人能解释一下为什么当我尝试运行2个或更多线程时,这个程序中的窗口会冻结吗 我希望能够在两个线程都运行时单击Hello按钮。 这是我的简单项目。 任务类只打印出单词“Testing” 关闭类在控制台内键入stop时停止线程 import java.util.Scanner; public class Closing implements Runnable{ Scanner s = new Scanner(System.in); public void run() { while (s.

有人能解释一下为什么当我尝试运行2个或更多线程时,这个程序中的窗口会冻结吗

我希望能够在两个线程都运行时单击Hello按钮。 这是我的简单项目。 任务类只打印出单词“Testing”

关闭类在控制台内键入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;
这里是主窗口所在的窗口类

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()
线程睡眠(5000)将影响到名为run的线程,另一方面,当您创建一个新线程时,睡眠将影响到这个线程

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){} 
        };
    }
}