簡體   English   中英

線程停止,掛起和恢復

[英]Threads stopping, suspending, and resuming

我正在編寫一個涉及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() ,我收到一條警告,提示“不贊成使用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) 

我不想要直接的答案,我想了解為什么我會遇到這些錯誤,以及是否應該經歷任何資源。 PS我搜索了很多答案,但沒有得到任何解釋此問題的信息。 謝謝

線程獲取對象上的鎖。 多線程的最重要部分是安全地交織線程,以便所有線程都可以使用資源(對象)。 如果處理不當,則會導致死鎖

使用stop() ,您正在殺死線程。 那個線程永遠消失了。 它可能導致處於停止狀態的對象(已停止線程已獲取)。

不建議使用suspend() ,因為一旦線程被掛起,其他線程將無法獲取資源,因為被掛起的線程對其保持了鎖定。

下圖描述了如何正確使用線程。 使用sleep()wait()notify()可以有效地交錯線程。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM