我真的很困惑:当我在eclipse中正常运行我的程序时,我的一些代码无法正常工作,但是当我使用调试模式单独运行每个步骤时,它确实工作了.
码:
public void showConnectDialog() {
ConnectDialog connectDialog = new ConnectDialog();
connectDialog.setVisible(true);
//Until here, code runs
while(! connectDialog.getConnected()) {};
//The next line does only run in debug
JOptionPane.showMessageDialog(connectDialog, "Connected", "Connected", JOptionPane.INFORMATION_MESSAGE);
}
Run Code Online (Sandbox Code Playgroud)
连接器(一旦用户在对话框中点击"连接")就启动(作为线程):
private class ServerConnector implements ActionListener, Runnable {
@Override
public void actionPerformed(ActionEvent e) {
if (! IP_field.getText().equals("")) {
if (! isConnecting) {
new Thread(new ServerConnector(), "ServerConnector").start();
}
}
else {
JOptionPane.showMessageDialog(dialog,
"Enter an IP address",
"Enter IP",
JOptionPane.WARNING_MESSAGE);
}
}
@Override
public void run() {
try {
setConnecting(true); …Run Code Online (Sandbox Code Playgroud) 有人可以解释Thread.yield()方法和Thread.sleep()方法之间的区别吗?
我是如何理解的:Thread.yield()放弃监视器锁定到JVM决定接下来执行的其他线程,Thread.sleep()并将当前线程置于睡眠模式一段给定的毫秒数而不放弃监视器锁定.