Wissam Eng
Wissam Eng

Reputation: 49

multithread in Java Rmi

I was wondering how to use threads with RMI. For example how to make the following Java RMI Simple code become multithreaded?

Interface:

public interface NntpServerInterface extends Remote
{
    String Hello() throws  RemoteException;
}

Interface implementation:

public class NntpServerImpl  extends UnicastRemoteObject implements NntpServerInterface
{
  public   NntpServerImpl() throws RemoteException
  {
  }

  public String Hello()
  {
      return "Hello" ;
  }
}

and where I can run main server:

public class NntpServerRun {
    static Registry reg;

    public static void main(String args []) throws RemoteException, AlreadyBoundException
    {
        reg=LocateRegistry.createRegistry(1111);
        reg.bind("NntpServer", new NntpServerImpl());
        System.out.println("Nntp Server  Started........");
    }
}

and finally the client side:

public class Client 
{
    static Registry reg;
    static NntpServerInterface ci;

    public static void main(String args )
    {
        reg=LocateRegistry.getRegistry(jTextField1.getText(), 1111);
        ci=(NntpServerInterface)(reg.lookup("NntpServer"));
        System.out.print(ci.Hello());
    }
}

Is it possible to change the above code to be multithreaded? I heard that RMI is already multithreaded.

Upvotes: 2

Views: 1113

Answers (1)

user207421
user207421

Reputation: 310915

I heard that RMI is already multithreaded.

You are correct. No change is necessary to this code. In general it is up to you to ensure thread-safety of your remote objects, but this particular one is already thread-safe.

Upvotes: 1

Related Questions