【问题标题】:Threads stopping, suspending, and resuming线程停止、挂起和恢复
【发布时间】:2013-11-29 15:37:47
【问题描述】:

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

  1. “开始”启动线程。
  2. "Stop" 停止线程。
  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() 时,我收到一条警告,提示“不推荐使用 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)
    

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

【问题讨论】:

标签: java multithreading swing


【解决方案1】:

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

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

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

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

【讨论】:

  • 我可以知道图片的参考吗?
猜你喜欢
  • 1970-01-01
  • 2023-03-22
  • 2012-05-20
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-17
  • 2013-07-20
相关资源
最近更新 更多