【问题标题】:multithread in Java RmiJava Rmi 中的多线程
【发布时间】:2014-06-13 19:56:36
【问题描述】:

我想知道如何在 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 已经是多线程的了。

【问题讨论】:

  • RMI 已经在使用多个线程并且它是线程安全的。所以你不需要做任何事情来使它成为多线程的。你能澄清一下你想要做什么吗?
  • @PeterLawrey 已经是多线程的了,但是线程安全在旁观者眼中,或者在这种情况下是程序员:-)

标签: java multithreading rmi


【解决方案1】:

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

你是对的。无需更改此代码。一般来说,确保远程对象的线程安全取决于您,但这个特定对象已经是线程安全的。

【讨论】:

  • 你为什么说'已经是线程安全的'?是不是因为他没有使用任何状态或静态字段?
  • @Teddy 因为他在远程方法实现中没有使用任何易失性非同步共享资源。
猜你喜欢
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
相关资源
最近更新 更多