Make all methods in BlackDeck and WhiteDeck be synchronized to make those classes self-threadsafe.
This commit is contained in:
parent
bc4f0818f1
commit
1860a96f81
|
@ -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();
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue