gpt4 book ai didi

java - 线程停止、挂起和恢复

转载 作者:行者123 更新时间:2023-11-30 07:12:32 25 4
gpt4 key购买 nike

我正在编写涉及 JFrame 和线程的代码。该线程应从文本字段中获取文本并将其写入文本区域。我有 4 个按钮,如下所示:

  1. “开始”以启动线程。
  2. 停止线程的“停止”。
  3. “暂停”暂停并继续线程。
  4. 和停止线程并退出程序的“退出”。

我已经创建了线程并在框架的构造函数中实现了“run()”函数,这里是:

WritingThread = new Thread(new Runnable() {
@Override
public void run() {
String s = WrittenText.getText();
while(true)
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < s.length(); j++)
{
WritingArea.append("" + s.charAt(j));
try {
Thread.sleep((int)ThreadSpeedSpinner.getValue());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
WritingArea.append("\n");
}
WritingArea.setText("");
}
}
});

这些是按钮:

JButton btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(!WritingThread.isAlive())
WritingThread.start();
}
});
JButton btnStop = new JButton("Stop");
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(WritingThread.isAlive())
WritingThread.stop();
}
});
btnPause = new JButton("Pause");
btnPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(!isPaused)
{
if(WritingThread.isAlive())
{
WritingThread.suspend();
btnPause.setText("Continue");
isPaused = true;
}
}
else
{
WritingThread.resume();
btnPause.setText("Pause");
}
}
});
JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
WritingThread.stop();
System.exit(0);
}
});

我有两个问题:

  1. 当我使用 stop()suspend()resume() 时,我收到警告说“The method from the Thread 类型已弃用。
  2. 当我运行程序时,我启动线程,然后停止它,然后尝试启动它我有这个异常

    Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
    at java.lang.Thread.start(Unknown Source)
    at com.HomeWork.HomeWork5$6.actionPerformed(HomeWork5.java:140)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

我不想要直接的答案,我想了解为什么会出现这些错误,以及是否有任何我应该查看的资源。附言我搜索了很多答案,但没有得到任何解释这个问题的信息。谢谢

最佳答案

线程获取对象的锁。而多线程最重要的部分就是把线程安全的交织起来,让所有的线程都可以使用这个资源(对象)。如果处理不当,会导致死锁

当您使用stop() 时,您正在终止线程。那条线永远消失了。它可能导致已停止的线程已获取的对象处于不一致状态

suspend() 已弃用,因为一旦线程被挂起,其他线程将无法获得资源,因为挂起的线程持有它的锁。

下图描述了应该如何正确使用线程。使用 sleep()wait()notify() 有效地交错线程。

enter image description here

关于java - 线程停止、挂起和恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20289156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com