Results 1 to 11 of 11
  1. Default Java threads and synchronization


    OK, I just don't understand how threads work. I don't know how I'm supposed to wake up a -specific- thread.

    I'm working on the implementation of a simple two-player card game. I have five classes: Card, CardDeck, Dealer, Player and CardGame.

    First, I ask for keyboard input for two Strings representing the names of the two players. I make two Players based on those names, and make a new CardDeck.

    The CardDeck contains an ArrayList of Cards, which are numbered 0-7. There are 4 cards with each number in the deck. The CardDeck is shuffled after its creation.

    Then an instance of Dealer takes the top 8 cards off CardDeck and gives each player four of the cards.

    Starting with player1, each player draws a card (now having five in their hand). The player's hand is printed out to the console and the player discards one of their cards, aiming to have four cards all with the same value (4 4 4 4, or 0 0 0 0). If player1 doesn't win at this point, player2 takes their turn and vice versa until one player wins.

    CardGame acts as the 'main' class and sets everything in motion. It starts three threads (dealer, player1 and player2) and waits for Dealer to give the signal that the game is over, after which it just cleans up and terminates.

    CardGame.java


    Player represents a player and implements Runnable. Each instance has a name and a hand, and a boolean showing whether or not they have won. I also pass in CardDeck and the Scanner object representing the keyboard in order to take keyboard input within the class.

    At the moment, the run() method does nothing. This is because I don't know how to implement the multithreading functionality into Player.

    Player.java


    The code for CardDeck might be necessary for the solution I'm asking for, so here it is. It's a simple ArrayList of instances of Card, and CardDeck contains the methods necessary for drawing cards from the top of the deck and discarding cards to the bottom of the deck.

    CardDeck.java


    Added the code for Dealer below. Dealer just represents the dealer and has one job: to deal four cards to each player at the beginning of the game. This code is inside its own class because I'm following the instructor (otherwise I'd just have deal() in CardGame).

    Dealer pretty much runs the entire game once its thread is started. This is because I haven't figured out the multithreading part of the exercise yet. Preferably, at the very end of Dealer's run() method it would notify CardGame to let it know that the game has finished.

    Dealer.java

    What's the correct implementation for this? After starting the two Player threads, Player1 should go first immediately. When Player1's turn is done (and Player1 hasn't won yet), Player2 should be notified and Player1 should go to sleep and vice versa. And of course, when either Player1 or Player2 hasWon, the main() thread should keep going.

    Any help would be greatly appreciated. I'm just having trouble grasping how this is supposed to work.
    Last edited by Jedward; 2014-02-27 at 06:24 PM.

  2. Default Re: Java threads and synchronization


    Why do you need threads?

    Adding concurrency will only complicate your program considering how simple it is.

  3. Default Re: Java threads and synchronization


    Assignment requirement.

  4. Default Re: Java threads and synchronization


    Not sure how to help you. You need to give more information. If this is your real code, I suggest fixing some of the errors in it too.

    Code:
    		while((!player1.hasWon) && (!player2.hasWon));
    is pretty much a logic error.

  5. Default Re: Java threads and synchronization


    Added full implementations of four of the classes to the OP, mess and all. I don't know what other information I can give; I'm cleaning up the explanation of how the program's supposed to work right now and will edit it into OP as soon as it's done. I put an overview of the game into the first part of the OP.

    @Locked did you spot any other errors? I don't actually think that loop is an error, but maybe you can tell me why I'm wrong. That loop just does nothing while checking if player1 or player2 have won, so as soon as either of the booleans return true (false with the negation), the loop ends and the last part of CardGame runs.

  6. Default Re: Java threads and synchronization


    The loop does nothing. It terminates because it reaches the semicolon.

    You say you want to wake a thread but none of the threads are asleep.\

    Ignore me.

    Use notify or notifyAll?

  7. Default Re: Java threads and synchronization


    So the loop solution is...

    Code:
    		while((!player1.hasWon) && (!player2.hasWon)){
    			
    		}
    ?

    none of the threads are asleep


    At the moment, when I run the code, nothing happens after the threads are started. Both are waiting to be woken up. What wakes them up, I don't know.

    I can't call notify or notifyAll from main, which means:
    • Dealer should be a thread and somehow notify player1 when it's done dealing?
    • notify should happen somewhere in the Player thread?
    • go() shouldn't wait and instead, the draw() and discard() methods in CardDeck should be synchronized instead?

  8. Default Re: Java threads and synchronization


    Yes

    Try notify or notifyAll.

    I never realised that `wait` is a form of sleeping in Javaland.

  9. Default Re: Java threads and synchronization


    @Jedward; Did you figure this out? You just need to figure out when threads need to wait and when they need to notify another thread.

    Dealer/CardGame/Main thread: Notify players when it's their turn. Wait during their turn.
    Player: Wait when it's not my turn. Notify dealer when my turn is over.


    Also there's an extra semicolon in Player.removeCard()
    Code:
    if(!(hand.remove(temp)));

  10. Default Re: Java threads and synchronization


    I've been stewing over this while working on other things these past few days.

    Game's working but not threaded properly. I keep getting IllegalMonitorStateExceptions when I try to implement the wait/notify system, assuming it's because I haven't figured out exactly where wait() and notify() should go in.

    I guess I still haven't figured out the question I posed at the very beginning of this thread: Can I wake up a specific thread instead of being at the mercy of luck? If so, how?

    The run() functions here are copypasted from the first post, with comments on where I would put wait() functions to help explain my thought process.

    CardGame.java


    Dealer.java


    Player.java


    Cleaned up first post and this post to reflect the new code. Currently the game works, but isn't actually using multithreading.

  11. Default Re: Java threads and synchronization


    You can wake up specific thread by using notify().
    E.g. When the dealer wants to wake up a waiting player 1, use player1.notify();
    When player 1 wants to wake up the dealer, use dealer.notify(); Players need to have a reference to the dealer of course.

  12.  

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •