点击暂停按钮,当线程没有暂停 [英] Thread isn't pausing when pause button is clicked

查看:201
本文介绍了点击暂停按钮,当线程没有暂停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,当用户点击暂停按钮暂停反弹球动画,并恢复它,当他们点击简历。问题是,当我点击暂停按钮使其进入if语句和运行这将导致线程等待从动画停止球while语句应该设置suspendRequested为true。然后,当用户点击恢复,应该把它设回假打破它while循环并继续制作动画。这不会发生,为什么它不破环了?

I am trying to pause a bouncing ball animation when the user clicks the pause button and resume it when they click resume. The problem is when I click the pause button it should be setting the suspendRequested to true so it enters the if statement and runs the while statement which causes the thread to wait which stops the ball from animating. Then when the user clicks resume it should set it back to false which breaks it out of the while loop and it continues to animate. This does not occur so why it isn't breaking out of the loop?

class BallRunnable implements Runnable
{
   private Lock suspendLock = new ReentrantLock();
   private Condition suspendCondition = suspendLock.newCondition();
   private volatile boolean suspendRequested = false;
   private boolean isBouncing = true;
   private Ball ball;
   private Component component;
   public static final int STEPS = 1000;
   public static final int DELAY = 100;

   /**
    * Constructs the runnable.
    * @param aBall the ball to bounce
    * @param aComponent the component in which the ball bounces
    */
   public BallRunnable()
   {
   }
   public BallRunnable(Ball aBall, Component aComponent)
   {
      ball = aBall;
      component = aComponent;
   }

   public void run()
   {
      while(isBouncing())
      {
          try
          {
                ball.move(component.getBounds());
                component.repaint();
                Thread.sleep(DELAY);
                String name = Thread.currentThread().getName();
                System.out.println("Ball is bouncing " + name);
                if(suspendRequested)
                {
                    suspendLock.lock();
                    System.out.println("Locking thread");
                    try
                    {
                        while(suspendRequested)
                        {
                            System.out.println("About to await");
                            suspendCondition.await();
                        }
                    }
                    catch (InterruptedException e)
                    {
                    }
                    finally
                    {
                     suspendLock.unlock();
                     System.out.println("Unlocked " + name);
                    }
                }
          }
          catch (InterruptedException e)
          {
          }

        }
    }

    public boolean isBouncing()
    {
        if(isBouncing)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public void setBouncing(boolean b)
    {
        isBouncing = b;
    }

   public void requestSuspend()
   {
       suspendRequested = true;
   }

   public void requestResume()
   {
       suspendRequested = false;
       suspendLock.lock();
       try
       {
           suspendCondition.signalAll();
       }
       finally
       {
           suspendLock.unlock();
       }
   }

}

这应该暂停和恢复线程都单击按钮​​时,但它不会破坏他们走出循环。如果这使得它看起来布尔设置为true,然后用户presses暂停将其更改为false,它不应该打破它圈外的?

Here it is supposed to pause and resume the thread when the buttons are clicked but it doesn't break them out of the loop. If the boolean that makes it look is set to true and then the user presses pause to change it to false shouldn't it break it out of the loop?

class BounceFrame extends JFrame
{
   BallRunnable br = new BallRunnable();
   private BallComponent comp;

   /**
    * Constructs the frame with the component for showing the bouncing ball and Start and Close
    * buttons
    */
   public BounceFrame()
   {
      comp = new BallComponent();
      add(comp, BorderLayout.CENTER);
      JPanel buttonPanel = new JPanel();
      addButton(buttonPanel, "Start", new ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               addBall();
               //Don't let user click again
               System.out.println("Clicked start");
            }
         });

    addButton(buttonPanel, "Pause", new ActionListener()
    {
        public void actionPerformed(ActionEvent event)
        {

            System.out.println("Clicked Paused");
            br.setBouncing(false);
            System.out.println("Clicked Paused STOP BOUNCinG");
            br.requestSuspend();
            String name = Thread.currentThread().getName();
            System.out.println("Clicked Paused REQUEST SUSPEND " + name);
        }
    });

    addButton(buttonPanel, "Resume", new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                System.out.println("Clicked Resume");
                br.setBouncing(true);
                br.requestResume();
            }
    });

      add(buttonPanel, BorderLayout.SOUTH);
      pack();
   }

这是家庭作业,所以我不是在寻找一个解决方案,但它是什么,我没有看到有关打破了暂停按钮被点击时的循环?

This is homework so I'm not looking for a solution but what is it I'm not seeing about breaking out of the loop when the pause button is clicked?

推荐答案

所以,其实我用的计时器对象,当你preSS按钮计时器就会启动,如果你再次点击比​​它会停下来,你需要一个定时器类,你弹跳球。其实我用pssed执行的操作方法鼠标$ P $,但它是一回事

So Actually i used the timer object, when you press the button timer will start, if you again clicked than it will stop, you need to make a timer class for you bouncing ball. Actually i used the mouse Pressed action performed method, but it is same thing

if (myTimer.isRunning())
    {
     myTimer.stop();
    }
    else
    {
     myTimer.start();
    }
    }
    });

这篇关于点击暂停按钮,当线程没有暂停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆