-2
public class RMIClient {

    public static void main(String[] args) {
        String serverURL = "rmi://" + args[0] + "/GameServer";
        String viewURL = "rmi://" + args[0] + "/ViewServer";

        try {
            GameInterface gameIntf = (GameInterface)Naming.lookup(serverURL);
            PlayerView view = (PlayerView)Naming.lookup(viewURL);
            while(!gameIntf.getGameOver()){
                synchronized(GameInterface.sharedObject){
                    GameInterface.sharedObject.notify();
                    System.out.println(view.getMessage());
                    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                    if(br.readLine().contains("y"))
                        gameIntf.setNextMove(true);
                    GameInterface.sharedObject.wait();
                }           
            }
        } catch (MalformedURLException | RemoteException | NotBoundException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }        
    }
}

public interface GameInterface extends Remote{

    public static final Object sharedObject = new Object();

    public void setNextMove(boolean val) throws RemoteException;

    public boolean getGameOver() throws RemoteException;
}

Currently when I start rmiregistry, server and start two RMI clients, both clients wait forever after getting 1st input i. e. one client is not notifying other. I am starting RMI clients on same JVM.

bman1779
  • 23
  • 6
  • 1
    Please format your question correctly. – Richard Erickson Nov 22 '15 at 02:28
  • From post: [Java RMI and Thread Synchronization questions](http://stackoverflow.com/questions/2275868/java-rmi-and-thread-synchronization-questions?lq=1) I concluded that above code should work. i. e. when multiple clients connect to Remote Object, synchronization is possible between them. – bman1779 Nov 22 '15 at 02:41
  • Your code might run correctly, but it does not appear to be formatted correctly for SO. All lines with code should be indented by 4 spaces. Please double check your post. – Richard Erickson Nov 22 '15 at 02:43

1 Answers1

0

No. The stub object at the client is not the same Java object as the remote server in the server host, and synchronizing on it or notifying it doesn't magically propagate across the network to the server or to other clients.

The answers in the link you cited don't support your theory either. You need to read the answers by @MarcH and me, and ignore the others, which are wrong in various ways.

Community
  • 1
  • 1
user207421
  • 305,947
  • 44
  • 307
  • 483