【问题标题】:RMI multithread skeletonRMI 多线程骨架
【发布时间】:2013-06-26 16:54:10
【问题描述】:

我的应用程序使用 rmi 服务客户端请求来操作数据库上的数据(使用 JDBC)。我希望该骨架为每个客户端的操作请求运行一个线程。我只需要像

public MySkeleton implement MyInterface {
    public string method1() {
        myThread.start();
    }

还是别的什么?

【问题讨论】:

  • 绝对不是run()。线程以Thread#start() 开头。
  • 好的,现在您的线程将启动并执行您在其中覆盖的run 方法,或者您在其构造函数中传递的Runnable 方法。
  • 只需将 Myinterface 实例传递给 Runnable impl 即可运行类似 link

标签: java multithreading rmi


【解决方案1】:

您不需要做任何特别的事情,RMI 框架会自动为您分拆新线程。尝试使用最简单的服务器,您会发现每次新客户端连接时,它总是能够直接连接到服务器:

public interface Server extends java.rmi.Remote {
    void doIt () throws java.rmi.RemoteException;
}

public class ServerImpl extends java.rmi.server.UnicastRemoteObject implements Server {
    public ServerImpl() throws java.rmi.RemoteException {
            super();
    }

    public void doIt () {
            System.out.println ("Starting in " + Thread.currentThread().getName());
            try { Thread.sleep(10000); } catch(InterruptedException t) {}
            System.out.println ("Stopping in " + Thread.currentThread().getName());
    }

    public static void main (String[] argv) throws Exception {
            java.rmi.registry.LocateRegistry.createRegistry(1099);
            java.rmi.Naming.rebind ("//localhost/Server", new ServerImpl ());
    }
}

public class Client {
    public static void main(String[] argv) throws Exception {
            ((Server) java.rmi.Naming.lookup("Server")).doIt();
    }
}

【讨论】:

    【解决方案2】:

    我希望该骨架为每个客户端的操作请求运行一个线程。

    RMI 已经这样做了。

    我只需要类似的东西

    不,你没有。正常写方法就好了。 RMI 将为您实现多线程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-13
      • 2019-02-01
      • 2015-07-16
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 2016-08-25
      • 1970-01-01
      相关资源
      最近更新 更多