单击暂停按钮时线程不会暂停

时间:2013-04-03 00:03:31

标签: java multithreading button

我试图在用户点击暂停按钮时暂停一个弹跳球动画,并在他们点击恢复时恢复它。问题是,当我单击暂停按钮时,它应该将suspendRequested设置为true,以便它进入if语句并运行while语句,这会导致线程等待,从而阻止球动画。然后,当用户单击“恢复”时,它应将其设置为“假”,将其从while循环中分解出来并继续进行动画处理。这不会发生,为什么它没有突破循环?

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,然后用户按下暂停将其更改为false,是否应该将其从循环中分离出来?

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();
   }

这是作业,所以我不是在寻找解决方案,但是当点击暂停按钮时,我没有看到突然出现的问题?

1 个答案:

答案 0 :(得分:0)

所以实际上我使用了计时器对象,当你按下按钮计时器将启动时,如果你再次点击它将停止,你需要为你弹出一个计时器类弹跳球。其实我用鼠标Pressed动作执行方法,但它是一样的

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