Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8743245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:38:32+00:00 2026-06-13T11:38:32+00:00

I am fairly new to JAVA and especially concurrency, so probably/hopefully this is fairly

  • 0

I am fairly new to JAVA and especially concurrency, so probably/hopefully this is fairly straight forward problem.

Basically from my main thread I have this:

public void playerTurn(Move move)
{

  // Wait until able to move
  while( !gameRoom.game.getCurrentPlayer().getAllowMove() )
  {
    try {
      Thread.sleep(200);
      trace("waiting for player to be available");
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
  gameRoom.getGame().handle(move);
}

gameRoom.getGame() is on its own thread.
gameRoom.getGame().handle() is synchronized
gameRoom.game.getCurrentPlayer() is on a varible of gameRoom.getGame(), it is in the same thread

allowMoves is set to false as soon as handle(move) is called, and back to true once it has finished processing the move.

I call playerTurn() multiple times. I actually call it from a SmartFoxServer extension, as and when it receives a request, often in quick succession.

My problem is, most times it works. However SOMETIMES it is issuing multiple handle(move) calls even though allowMoves should be false. Its not waiting for it to be true again. I thought its possible that the game thread didn’t have a chance to set allowMoves before another handle(move) was called. I added volatile to allowMoves, and ensured the functions on the game thread were set to synchronized. But the problem is still happening.

These are in my Game class:

synchronized public void handle(Object msg)
{
  lastMessage = msg;
  notify();
} 

synchronized public Move move() throws InterruptedException
{
  while (true)
  {
   allowMoves = true;
   System.out.print(" waiting for move()...");
   wait();
   allowMoves = false;
   if (lastMessage instanceof Move)
   {
    System.out.print(" process move()...");
    Move m = (Move) lastMessage;
    return m;
   }
  }
}

public volatile boolean allowMoves;
synchronized public boolean getAllowMoves()
{
  return allowMoves;
}

As I said, I am new to this and probably a little ahead of myself (as per usual, but its kinda my style to jump into the deep end, great for a quick learning curve anyway).

Cheers for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:38:33+00:00Added an answer on June 13, 2026 at 11:38 am

    Not sure if this will help, but what if you will use AtomicBoolean instead of synchronized and volatile? It says that it is lock-free and thread-safe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Exception in thread “main” java.lang.NoSuchMethodError: main I am fairly new to Java,
I'm still fairly new to Java, but I'm fairly sure this shouldn't happen. There's
First of all, I'm fairly new to Java, so sorry if this question is
I'm still fairly new to Python, and my OO experience comes from Java. So
I'm fairly new to Java and I thought this worked the same as with
I am fairly new to java so bear with me please, basically, below I
I am fairly new to java having come from a VS2010 background. Currently working
I am fairly new to Java so I am probably missing something fundamental here
I'm a fairly new to Java and to programming, so excuse me if this
I'm fairly new to Java, and I cannot seem to solve this NotSerializableException that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.