I have a program where players play a game and the user can input p or r to pause and resume the game, using event listeners,and this functionality works in java 6 but not in java 7, and I don't understand why not. could someone please help.
Event listener thread
public class MyKeyListenerThread extends Thread implements MyKeyListener{
/**
* This is the overriden keyPressedEvent method, used
* to respond to [@link MyKeyPressed] events and pause or resume
* [@link Player] threads according to the source details.
*
* @param evt The event the listener will respond to
*/
@Override
public void keyPressedEvent( MyKeyEvent evt ){
Object o = evt.getSource();
//checks source to see whether to pause or resume game
if( o.equals("p") ){
for ( Player player : CardGame.players ){
player.setPaused( true );
Logger.addToOutput( player.getPlayerName(), "The game has been paused\n" );
}
System.out.println( "The game has been paused" );
}else if ( o.equals("r") ){
for ( Player player : CardGame.players ){
player.setPaused( false );
Logger.addToOutput( player.getPlayerName(), "The game has been resumed\n" );
}
System.out.println( "The game has been resumed" );
}
}
/**
* This is the overidden run method, which will
* run infinitely in the background.
*/
@Override
public void run(){
for(;;) {
}
}
}
System.out.println( "The game has been paused" );
}else if ( o.equals("r") ){
for ( Player player : CardGame.players ){
player.setPaused( false );
Logger.addToOutput( player.getPlayerName(), "The game has been resumed\n" );
}
System.out.println( "The game has been resumed" );
}
}
The player thread
public class Player implements Runnable{
LinkedList<Card> hand;
String playerName;
int strategyChosen;
int playerNumber;
private boolean running = true;
private boolean paused = false;
public void setPaused( boolean paused ){
this.paused = paused;
}
public synchronized void run(){
while(running){
if(!paused){
playGame();
}
}
}
"event generator"
MyKeyListenerThread elt = new MyKeyListenerThread();
CardGame.addMyKeyListener( elt );
elt.start();
for ( int i = 0; i < players.length; i++){
new Thread( players[i] ).start();
}
while(running){
String input = reader.next();
CardGame.pressed = true;
fireMyKeyEvent( new MyKeyEvent(input) );
}
private static void addMyKeyListener( MyKeyListener t) {
MyKeyListener[] tmp = new MyKeyListener[listeners.length+1];
for (int i = 0; i < listeners.length; i++)
tmp[i] = listeners[i];
tmp[listeners.length] = t;
listeners = tmp;
}
private static void fireMyKeyEvent(MyKeyEvent evt) {
for ( MyKeyListener keyListener : listeners ) {
keyListener.keyPressedEvent(evt);
}
}
I can get the program to pause but I cannot get it to resume even though it prints to the terminal the "The game has been resumed", but it resumes normally in java 6. what changed in java 7, or is it just my code.Help would be appreciated.
Try making your paused
variable volatile
.
Variables which are intended to be accessed between threads should be marked volatile
in order to indicate that the values should not be cached between threads. Without this flag, it's possible for a thread to not see that a value was changed in another thread.
The functionality and purpose of this flag is somewhat confusing, but there are lots of resources out there explaining it: https://www.google.com/search?q=java+volatile
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With