我在下面定义了 2 个类:
public class TextsManager extends Thread {
LinkedList<String> lstOfPendingStr = new LinkedList<String>();
boolean stopLoop = false;
JTextArea txtArea;
public void run()
{
while (!stopLoop)
{
while (!lstOfPendingStr.isEmpty())
{
String tmp = lstOfPendingStr.getFirst();
this.txtArea.append(tmp);
lstOfPendingStr.removeFirst();
}
try {
Thread.sleep(0); // note: I had to force this code
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void AddNewStr(String newStr)
{
this.lstOfPendingStr.add(newStr);
}
}
和
public class ClientApp {
private JFrame frame;
private JTextField textField;
private JTextArea textArea;
static private TextsManager txtManager;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ClientApp window = new ClientApp();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ClientApp() {
initialize();
/*
* Client app
*/
txtManager = new TextsManager(textArea);
txtManager.start();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
textArea = new JTextArea();
textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
txtManager.AddNewStr(textField.getText() + "\n");
textField.setText("");
}
}
});
}
}
该程序将从 中读取用户输入textField
,并将其传递给TextsManager.lstOfPendingStr
. 然后,在 () 内部的每个循环中TextsManager.run
,它会检查其中存在的成员lstOfPendingStr
并通过txtArea
.
问题是,如果我删除了Thread.sleep(0)
里面的代码run()
,run()
那么显然就停止了工作。尽管lstOfPendingStr
已经成功更新了新元素,但循环内的代码while(!lstOfPendingStr.isEmpty())
永远不会被调用。
我将硬代码如System.out.println
or Thread.sleep(0)
(如提供的代码中)放在 内while(!stopLoop)
,然后它工作正常。
虽然,我设法通过强制线程休眠几毫秒来解决问题,但我想知道这个问题背后的原因。
我很欣赏你的智慧。
看待 :)