【问题标题】:How can I pause/continue scheduled task?如何暂停/继续计划任务?
【发布时间】:2020-03-19 16:11:23
【问题描述】:

我是 JAVA 新手,正在尝试学习一些并发概念。
我有一个简单的 GUI 类,它会弹出一个带有 1 个按钮的窗口,我想用它来暂停/继续。
另外,我有一个扩展 TimerTask 的类,它看起来像下面并从 GUI 开始:

public class process extends TimerTask {
  public void run() { 
     while(true) { /*some repetitive macro commands.. */ }
  }
} 

真正的问题是,如果已经暂停,如何暂停按钮的 onClick 任务并继续按钮的 onClick?

我已采取步骤使用布尔值来标记按钮,因为它会在每次单击时从暂停变为继续。

但后来我不得不输入很多 while(button); 来忙于在 while() 内等待...
你认为我可以像Thread.sleep() 或其他东西,但不是从任务线程之外?

【问题讨论】:

    标签: java


    【解决方案1】:

    老答案

    TimerTask上基本不支持暂停和恢复,只能取消,查看here 也许您可能想阅读有关线程的信息,因为这是我所知道的具有中断和启动功能的替代方案,然后您可以跟踪您正在执行的操作的进度以从停止的位置恢复。

    所以,我建议你通过this链接,因为你需要了解线程,而不仅仅是复制代码来使用,那里有一个示例代码也肯定会解决你的问题。

    请注意,运行一个无休止的 while 循环基本上会导致您的程序没有响应,除非系统崩溃。到了某一点,数据就变成了过载,程序就会溢出。这意味着它会失败。

    .

    新答案

    因此,针对新问题,我能够运行一个很小的程序来演示如何在使用 SWING 时实现看起来像多线程的东西。

    改写你的问题:你想运行一个无限期的任务,比如假设我们正在播放一首歌曲,然后点击一个按钮来暂停歌曲,再次点击应该继续歌曲吗?如果是这样,我想下面的小程序可能对你有用。

        public class Test{
    
            static JLabel label;
            static int i = 0;
            static JButton action;
            static boolean x = false; //setting our x to false initialy
    
            public static void main(String[] args) { 
                JFrame f=new JFrame();//creating instance of JFrame  
    
                label = new JLabel("0 Sec"); //initialized with a text 
                label.setBounds(130,200,100, 40);//x axis, y axis, width, height  
    
                action=new JButton("Play");//initialized with a text 
                action.setBounds(130,100,100, 40);//x axis, y axis, width, height  
    
                f.add(action);//adding button in JFrame  
                f.add(label);
    
    
                action.addActionListener(new ActionListener(){
    
                @Override
                public void actionPerformed(ActionEvent e){
    
                    if(x){                
                        x = false; //Update x here
    
                        action.setText("Play");
                        action.revalidate();
                    }else{
                        x = true; //Update x here also
    
                        action.setText("Pause");
                        action.revalidate();
    
                        if(x){ //Using x here to determind whether we should start our child thread or not.
                        (new Thread(new Child())).start();
                        }
                    }
                }
            });
    
                f.setSize(500, 700);//500 width and 700 height  
                f.setLayout(null);//using no layout managers  
                f.setVisible(true);//making the frame visible  
            }
        } 
    
        class Child implements Runnable{
    
            @Override
            public void run() {
                   while (x) { 
    
                    //You can put your long task here.
    
                    i++;        
                    label.setText(i+" Secs");
                    label.revalidate();
    
                    try {
                        sleep(1000); //Sleeping time for our baby thread ..lol
                    } catch (InterruptedException ex) {
                        Logger.getLogger("No Foo");
                    }
                } 
            }
        }
    

    【讨论】:

    • 嗨,这真的很有趣,谢谢。你能解释一下这个注释,为什么我的程序会崩溃以及如何预防吗?我只想循环执行一些宏命令几个小时,直到我希望它停止。
    • @Tezro 你在使用 SWING 作为 GUI 吗?
    • 是的,我使用 intellij 和 swing 作为 gui。
    • 我会推荐你​​通过docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html Swing 本身是单线程的,这可能是一个阻塞。同时,我会更新上面的答案。
    • @Tezro 我将修改您上面的问题以适应主题。
    猜你喜欢
    • 1970-01-01
    • 2013-05-20
    • 2017-02-15
    • 1970-01-01
    • 2012-07-18
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多