ユーザーが一時停止ボタンをクリックすると弾むボールのアニメーションを一時停止し、再開をクリックすると再開しようとしています。問題は、一時停止ボタンをクリックすると、suspendRequested を true に設定する必要があるため、if ステートメントに入り、while ステートメントを実行してスレッドを待機させ、ボールのアニメーションを停止させることです。次に、ユーザーが再開をクリックすると、それを false に戻す必要があります。これにより、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();
}
これは宿題なので、解決策を探しているわけではありませんが、一時停止ボタンがクリックされたときにループから抜け出すことについて私が見ていないのは何ですか?