线程停止,暂停和恢复

时间:2013-11-29 15:37:48

标签: java multithreading swing

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

  1. “开始”以启动主题。
  2. 停止线程的“停止”。
  3. “暂停”暂停并继续线程。
  4. 和“退出”停止线程并退出程序。
  5. 我已经在frame的构造函数中创建了线程并实现了“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)
      
    3. 我不想直接回答,我想了解为什么我会收到这些错误,如果有任何资源我应该通过。 附:我搜索了很多答案,并没有得到任何解释这个问题。 感谢

1 个答案:

答案 0 :(得分:4)

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

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

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

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

enter image description here

相关问题