当前位置: 首页 > 面试题库 >

如何从另一个线程暂停和恢复Java中的线程

管炳
2023-03-14
问题内容

我正在用Java Swing编写应用程序。我需要的是一个可以使用图形界面中的按钮停止“阐述”线程的过程。

这里有一个简单的项目专注于我的需求

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextArea;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Nikola
 */
public class Main extends javax.swing.JFrame
{
    private MyThread THREAD;

    public Main()
    {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Pause Thread");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton2.setText("Resume Thread");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Start Thread");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE)
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt)
    {
        THREAD = new MyThread(jTextArea1);
        THREAD.start();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    {
        try
        {
            THREAD.pauseThread();
        }
        catch (InterruptedException ex)
        {
            ex.printStackTrace();
        }
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
    {
        THREAD.resumeThread();
    }

    public static void main(String args[])
    {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
            {
                if ("Nimbus".equals(info.getName()))
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        }
        catch (ClassNotFoundException ex)
        {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (InstantiationException ex)
        {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable()
        {

            public void run()
            {
                new Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration
}

class MyThread extends Thread
{
    JTextArea area;

    private final Object lock = new Object();

    public MyThread(JTextArea area)
    {
        super();
        this.area = area;
    }

    @Override
    public void run()
    {
        for(int i=0 ; ; i++)
            area.setText(i+"");
    }

    public void pauseThread() throws InterruptedException
    {
        synchronized(lock)
        {
            lock.wait();
        }
    }

    public void resumeThread()
    {
        synchronized(lock)
        {
            lock.notify();
        }
    }
}

问题很简单:在实际的应用程序中,用户设置一些选项,然后启动线程,对选定的数据进行详细说明。

我想提供一个“暂停”按钮,以便用户可以暂时停止详细说明并进行必要的检查,然后可以恢复操作。

我编码的方式是停止的图形线程,而不是“精化”线程。

如果运行示例代码并按“开始”,则文本区域开始计数。我需要的最终结果是,当我按下“暂停”按钮时,线程进入“睡眠”状态,计数停止;当我按下“恢复”按钮时,线程“唤醒”,文本区域中的计数开始再数一次。


问题答案:

您无法按照您想要的方式 最终 从另一个线程中暂停一个线程

您需要做的是通过设置某种标志来 通知 另一个线程应该停止。有问题的线程必须具有检查该标志并在发生这种情况时暂停其工作的逻辑。

因此,在这种特殊情况下,可能MyThread如下更改:

class MyThread extends Thread {

    private volatile boolean running = true; // Run unless told to pause

    ...

    @Override
    public void run()
    {
        for(int i=0 ; ; i++)
        {
            // Only keep painting while "running" is true
            // This is a crude implementation of pausing the thread
            while (!running)
                yield;

            area.setText(i+"");
    }

    public void pauseThread() throws InterruptedException
    {
        running = false;
    }

    public void resumeThread()
    {
        running = true;
    }

}

这是一个粗略的例子,为简洁起见,它使用了一种自旋锁,而不是基于监视器的适当睡眠。希望它传达了您如何使用标志来控制线程暂停的想法。

请注意,如果您正在块中执行一些长时间运行的步骤集,而不仅仅是setText调用,那么最好Thread.currentThread().interrupted()在每个步骤之间进行检查-
如果设置了itnerrupt标志,则退出循环。广义上讲,这是内置阻塞方法(例如I / O)所做的,以便它们可以被其他线程中断-
由于该running标志仅在每个循环中被检查一次,因此在每个循环中对其进行设置都无济于事需要20分钟。



 类似资料:
  • 问题内容: 我声明我已阅读有关线程的内容,但从未使用过。所以我问你:) 我有两个线程:和,其中管理GUI和逻辑。 我将从开始。 然后在绘制GUI时,我将其暂停,以等待到达X点的run方法。 当到达X点进入run方法时,我暂停并继续。 并共享一些变量来管理GUI和逻辑… 我可以做吗?如果是,如何?:) 问题答案: 使用和方法: -使当前线程等待,直到另一个线程调用 该对象的方法。 -唤醒正在该对象的

  • 问题内容: 有没有一种方法可以使用ExecutorService暂停/恢复特定线程? 想象一下,我想停止id == 0的线程(假设为每个线程分配了一个增量ID,直到达到线程池的大小为止)。 过了一会儿,通过按下一个按钮,我想恢复该特定线程,并将所有其他线程保留为当前状态,这些状态可以暂停或恢复。 我在Java文档中发现了PausableThreadPoolExecutor的未完成版本。但这不适合我

  • 当onPause发生时,我的线程没有停止,导致“线程已启动”logcat错误onResume,因为我无法运行两个线程实例。此时如何终止线程?我认为我需要做一些事情,比如: 但是我不能把它添加到我的气球基本暂停中,我认为上下文是错误的。所以请帮忙,代码如下所示。(代码示例会很有帮助,谢谢) 我的活动: 我的表面视图: 我的线程:公共类 GameLoopThreadBasic 扩展线程 { 私有游戏视

  • 问题内容: 我向Java的executorservice提交了一堆作业,但我想以某种方式暂时暂停所有这些作业。最好的方法是什么?我该如何恢复?还是我这样做完全错误?我是否应该遵循其他想要实现的模式(即暂停/恢复执行服务的能力)? 问题答案: 为了回答我自己的问题,我在 本身的javadocs中找到了一个示例。这是我使用番石榴显示器的版本:

  • 我正在 Canvas 中创建一个示例游戏到表面视图中。 我正在使用更新和绘图方法,所有工作。现在我试图暂停并恢复游戏。我在网上找到的方法奏效了,但是当游戏恢复时,它要慢得多。 我的循环游戏: 我的暂停和恢复方法是: 有人知道为什么慢下来吗?以及解决方案?(注:我试图给出一个固定的睡眠而不是变量,结果是同样的慢) 谢谢。 @编辑 我发现了问题。 我的活动在Resume()上有一个监听器,这会启动线程

  • 嗨,我在camel中有一个JMS消费者路由,我的要求是在特定事件时停止/暂停该路由(基于某个字段值),然后使用调度器恢复该路由。为此,我创建了两个路由,一个是我的原始jms消费者路由,另一个是调度程序路由,它们恢复jms消费者路由,虽然我能够暂停路由,但第二个路由不恢复暂停的路由,它显示的状态为已启动。 以下是我的两条路线 请帮助我如何实现上述场景。