我在为架构编写原型时遇到了一个奇怪的问题。为什么启动一个线程被另一个线程阻塞?
我试着创建两个独立调度相同命令的线程。第一个线程是使用Scanner
,第二个线程是依靠Swing。问题在于第一个线程阻止了第二个线程的启动。第二个线程只有在扫描仪得到足够的输入后才开始。强制第一个线程睡眠,直到第二个线程开始也暂时解决问题。
以下示例非常一致地重现了此行为。睡在电话之间使得它更加一致。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public final class Bug {
public static void main(final String[] arguments) {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("The commands are \"wait\" and \"quit\".");
final Scanner scanner = new Scanner(System.in);
loop: while (true) {
System.out.print("Enter a command: ");
final String command = scanner.nextLine();
switch (command.toLowerCase()) {
case "exit":
case "quit":
break loop;
default:
System.out.println("Use \"wait\" or \"quit\" instead of \"" + command + "\".");
case "wait":
}
}
scanner.close();
}
}).start();
try {
Thread.sleep(1000);//improves consistency
}
catch (final InterruptedException exception) {}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
final JFrame frame = new JFrame("Commands");
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setResizable(false);
frame.setLayout(new BorderLayout());
frame.add(new JButton("Wait"), BorderLayout.LINE_START);
final JButton button = new JButton("Quit");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent event) {
frame.dispose();
}
});
frame.add(button, BorderLayout.LINE_END);
frame.pack();
frame.setVisible(true);
}
});
}
}
为什么没有第二个线程正常启动?我该责怪吗?
A similar problem是在十年前作为错误提交的。
运行java -version
结果
java version "1.7.0_21"
Java(TM) SE Runtime Environment (build 1.7.0_21-b11)
Java HotSpot(TM) Client VM (build 23.21-b01, mixed mode, sharing)
和cmd -info
在
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
如果该事项。
我在上面发布的代码中没有看到任何内容,可以解释您描述的行为。这对我来说是一个难题。 – 2013-04-26 20:49:18
适用于我:Ubuntu 13.04; openjdk 1.7.0_21 – skirsch 2013-04-26 20:49:41
我在Windows 7上试过这个代码,jdk 1.7。有用。你的情况的问题是别的。 – CodeBlue 2013-04-26 20:50:38