Java Rmi中的多线程

时间:2014-04-28 10:08:29

标签: java multithreading rmi

我想知道如何在RMI中使用线程。例如,如何使以下Java RMI Simple代码成为多线程?

界面:

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

接口实现:

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

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

我可以在哪里运行主服务器:

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........");
    }
}

最后是客户端:

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());
    }
}

是否可以将上述代码更改为多线程?我听说RMI已经是多线程的。

1 个答案:

答案 0 :(得分:1)

  

我听说RMI已经是多线程的。

你是对的。此代码无需更改。通常,您需要确保远程对象的线程安全,但这个特定的已经是线程安全的。