Make all methods in BlackDeck and WhiteDeck be synchronized to make those classes self-threadsafe.

This commit is contained in:
Andy Janata 2012-03-22 23:41:33 -07:00
parent bc4f0818f1
commit 1860a96f81
2 changed files with 10 additions and 6 deletions

View File

@ -37,6 +37,8 @@ import org.hibernate.Transaction;
/**
* Deck of Black Cards.
*
* This class is thread-safe.
*
* @author Andy Janata (ajanata@socialgamer.net)
*/
public class BlackDeck {
@ -72,7 +74,7 @@ public class BlackDeck {
* @throws OutOfCardsException
* There are no more cards in the deck.
*/
public BlackCard getNextCard() throws OutOfCardsException {
public synchronized BlackCard getNextCard() throws OutOfCardsException {
if (deck.size() == 0) {
throw new OutOfCardsException();
}
@ -88,7 +90,7 @@ public class BlackDeck {
* @param card
* Card to add to discard pile.
*/
public void discard(final BlackCard card) {
public synchronized void discard(final BlackCard card) {
if (card != null) {
discard.add(card);
}
@ -97,7 +99,7 @@ public class BlackDeck {
/**
* Shuffles the discard pile and puts the cards under the cards remaining in the deck.
*/
public void reshuffle() {
public synchronized void reshuffle() {
Collections.shuffle(discard);
deck.addAll(0, discard);
discard.clear();

View File

@ -37,6 +37,8 @@ import org.hibernate.Transaction;
/**
* Deck of White Cards.
*
* This class is thread-safe.
*
* @author Andy Janata (ajanata@socialgamer.net)
*/
public class WhiteDeck {
@ -72,7 +74,7 @@ public class WhiteDeck {
* @throws OutOfCardsException
* There are no more cards in the deck.
*/
public WhiteCard getNextCard() throws OutOfCardsException {
public synchronized WhiteCard getNextCard() throws OutOfCardsException {
if (deck.size() == 0) {
throw new OutOfCardsException();
}
@ -88,7 +90,7 @@ public class WhiteDeck {
* @param card
* Card to add to discard pile.
*/
public void discard(final WhiteCard card) {
public synchronized void discard(final WhiteCard card) {
if (card != null) {
discard.add(card);
}
@ -97,7 +99,7 @@ public class WhiteDeck {
/**
* Shuffles the discard pile and puts the cards under the cards remaining in the deck.
*/
public void reshuffle() {
public synchronized void reshuffle() {
Collections.shuffle(discard);
deck.addAll(0, discard);
discard.clear();