Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Why isn't my synchronized function acting synchronized?

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Why isn't my synchronized function acting synchronized?

    I have a multi-threaded network/client program that plays a game of tic-tac-toe.

    I always begin by asking the client if he's a new or existing player.

    If he's a new player, I ask him what he wants his username and symbol to be, and then I create a new player. I also check to make sure the username isn't already taken.


    Now, since both players are playing concurrently in their own thread, it is feasible that two player connect with each other, each of which are new players, and then both choose to create the same username.

    Since one player may still be "being created", his username won't be in the file yet. Which means that even though the second player chooses the same username, the game will think it's unused / valid because the server hasn't written the first player's username to the file yet.

    Here's a shortened version of the code:

    public void run()
                    {
                            try
                            {
                                    outputToClient.writeInt(1);
    // signal the client both players are connected and it's time to establish the players
     
     
                                    processMessage(input.readLine());
    // client tells server whether user is new or existing
    // server responds by accordingly creating or retrieving new player
     
     
                                    outputToClient.writeInt(playerNumber);
     
                            } catch (IOException ex)
                            {
                                    System.err.println(ex);
                            }
    }
     
    public synchronized void processMessage(String message)
    {
            if (message.equals("new"))
            {
                     try
                     {
                              boolean new_name = true;
                              do
                              {
                                      name = input.readLine();
                                      new_name = checkNewName(name);
     
                                      if (new_name)
                                      {
                                              System.out.println("The new name is : " + name);
                                              outputToClient.writeBytes("Name not taken\n");
                                       }
                                       else
                                       {
                                              System.out.println("The taken name is : " + name);
                                              outputToClient.writeBytes("Name taken\n");
                                        }
                              } while(!new_name);
     
                              symbol = input.readLine().charAt(0);
     
                      } catch (IOException ex)
                      {
                               System.err.println(ex);
                      }
     
                      createPlayer();
              }
              else
              {
                      String clientName = "";
                      try
                      {
                             clientName = input.readLine();
     
                       } catch (IOException ex)
                       {
                              System.err.println(ex);
                       }
     
                       retrievePlayer(clientName);
                }
     
                Symbols[playerNumber] = symbol;
    }


    The idea here is that by making "processMessage" synchronized, only one thread will be giving a name / creating a player at a time, so we know there will be no overlap. Yet it's not working....

    What am I doing wrong?


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Why isn't my synchronized function acting synchronized?

    Quote Originally Posted by ineedahero View Post
    What am I doing wrong?
    The code is not complete .... so I can only guess. I see that processMessage is an "instance" method and it's synchronized. This means that the lock is acquired on the object on which processMessage is invoked.

    run() and processMessage() seems in the same class, presumably a Runnable implementation or a Thread extension. And here is the difference.
    1) If it's a java.lang.Thread extension, obviously you have N instances, one for each thread.
    2) If it's a Runnable implementation, you may have created:
    2a) 1 instance, passed to all java.lang.Thread objects.
    or
    2b) N instances, one for each java.lang.Thread object.

    In case 1) and 2b) you have different objects = different locks, so no mutual exclusion.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    ineedahero (December 20th, 2013)

  4. #3
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: Why isn't my synchronized function acting synchronized?

    Sorry for the confusion.

    run() and processMessage() are both in the player class. The player class implements runnable.

    The player class itself is a part of the server class.

    It seems like I'm in 2b.

    If I put the processMessage method outside of the player class, and made it a method of the server, would that create mutual exclusion?

    And yes, I have two separate players, each running concurrently in their own thread in the run() method.

  5. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Why isn't my synchronized function acting synchronized?

    Quote Originally Posted by ineedahero View Post
    If I put the processMessage method outside of the player class, and made it a method of the server, would that create mutual exclusion?
    If you have 1 instance of server (and I suppose it is so), yes technically same object = same lock = mutual exclusion.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  6. The Following User Says Thank You to andbin For This Useful Post:

    ineedahero (December 20th, 2013)

Similar Threads

  1. synchronized
    By hs82 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 15th, 2013, 07:02 PM
  2. synchronized block
    By me_shankara in forum Threads
    Replies: 2
    Last Post: April 27th, 2013, 12:26 PM
  3. [SOLVED] Help with SYNCHRONIZED
    By mds1256 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: January 8th, 2012, 09:44 AM
  4. Synchronized block vs Synchronized method
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: April 20th, 2011, 07:51 AM
  5. Synchronized Collection
    By tcstcs in forum Java Theory & Questions
    Replies: 2
    Last Post: March 29th, 2011, 12:09 AM