Java 线程停止、挂起和恢复

Java 线程停止、挂起和恢复,java,multithreading,swing,Java,Multithreading,Swing,我正在编写一个包含JFrame和线程的代码。 线程应该从文本字段中获取文本并将其写入文本区域。 我有4个按钮,如下所示: 开始启动线程。 使线停止的停止。 暂停,暂停并继续线程。 和Exit,停止线程并退出程序。 我在frame的构造函数中创建了线程并实现了run函数,如下所示: WritingThread = new Thread(new Runnable() { @Override public void run() { String s = WrittenT

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

开始启动线程。 使线停止的停止。 暂停,暂停并继续线程。 和Exit,停止线程并退出程序。 我在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("");
        }
    }
});
我出现了两个问题:

当我使用stop、suspend或resume时,会收到一条警告,指出类型Thread中的方法已弃用。 当我运行程序时,我启动线程,然后停止它,然后尝试启动它。我有这个异常

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有效地交错线程


线程在对象上获得锁。也是多线程最重要的部分 是安全地交织线程,以便所有线程都可以使用资源对象。 如果处理不当,会导致死锁

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

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

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


避免在线程上使用stop ans suspend,一旦线程停止,就无法重新启动它。暂挂已弃用。阅读关于线程的基本Oracle联机手册:阅读关于这些方法的API文档。它解释了这些方法被弃用的原因,还解释了您无法重新启动线程。线程不像视频,您可以随意启动/停止。我不久前就开始讨论这个问题。避免在线程上使用停止和挂起。一旦线程停止,您就无法重新启动它。暂挂已弃用。阅读关于线程的基本Oracle联机手册:阅读关于这些方法的API文档。它解释了这些方法被弃用的原因,还解释了您无法重新启动线程。线程不像视频,您可以随意启动/停止。我不久前就开始讨论这个问题。我可以知道图像的引用吗?我可以知道图像的引用吗?
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)