Java 如何暂停/继续计划的任务?

Java 如何暂停/继续计划的任务?,java,Java,我是JAVA新手,正在尝试学习一些并发概念。 我有一个简单的GUI类,它会弹出一个带有1个按钮的窗口,我想用它来暂停/继续。 另外,我有一个扩展TimerTask的类,它如下所示,从GUI开始: public class process extends TimerTask { public void run() { while(true) { /*some repetitive macro commands.. */ } } } 真正的问题是,我如何在单击按钮时暂停任务,

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

public class process extends TimerTask {
  public void run() { 
     while(true) { /*some repetitive macro commands.. */ }
  }
} 
真正的问题是,我如何在单击按钮时暂停任务,如果已经暂停,如何继续单击按钮

我已经采取了一个步骤,使用布尔值来标记按钮,因为每次单击按钮时,按钮从暂停变为继续

但后来我不得不在while(按钮)中键入大量的
用于在
while()内忙着等待。

你认为我可以在任务线程之外制作类似于
Thread.sleep()
的东西吗

旧答案

基本上,TimerTask不支持暂停和恢复,您只能取消、检查 也许你想了解线程,因为据我所知,线程是一种具有中断和启动功能的替代方案,这样你就可以跟踪正在执行的操作的进度,以恢复到停止的位置

所以,我建议你们通过这个链接,因为你们需要理解线程,不仅仅是复制一个代码来使用,还有一个示例代码肯定会解决你们的问题

请注意,运行无休止的while循环基本上会导致程序不响应,除非系统崩溃。在某一点上,数据会过载,程序会溢出。这意味着它将失败

新答案

因此,作为对新问题的回答,我能够运行一个小程序来演示如何在使用SWING时实现类似多线程的功能

重新表述你的问题:你想运行一个不确定的任务,比如说我们正在播放一首歌,然后点击一个按钮暂停这首歌,再次点击应该继续这首歌吗?如果是这样,我认为下面的小程序可能适合你


嗨,真有趣,谢谢。你能解释一下这个注释吗,为什么我的程序会崩溃,以及如何防止崩溃?我只想让一些宏命令在循环中执行几个小时,直到我想让它停止为止。@Tezro你在GUI中使用SWING吗?是的,我在GUI中使用intellij和SWING。我建议你通过SWING本身是单线程的,这可能是一个拦截器。同时,我会更新上面的答案。@Tezro我会修改你上面的问题,以适应主要主题。
    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");
                }
            } 
        }
    }