使用多线程实现RMI服务器的哪个部分

时间:2018-06-06 11:42:29

标签: java multithreading rmi runnable

我最近刚刚启动了RMI编程,并完成了大部分的分配要求。但是在我的脑海中有一个问题,其中一个要求是支持多个线程。基本上应该在哪个部分实现多线程? 我应该使Server类Runnable还是在Server类Runnable中创建正在创建的对象? 这是我的服务器类的代码:

public class Server {

public static void main(String args[]) {
    String portNum = "4444";
    try {
        Account_Server_Controller accountController = new Account_Server_Controller();

        BookCategory_Server_Controller categoryController = new BookCategory_Server_Controller();

        Book_Server_Controller bookController = new Book_Server_Controller();

        BookActivity_Server_Controller bookActivityController = new BookActivity_Server_Controller();

        startRegistry(Integer.parseInt(portNum));
        Naming.rebind(ServerLocater.getAccountRegistryURL(), accountController);
        Naming.rebind(ServerLocater.getBookRegistryURL(), bookController);
        Naming.rebind(ServerLocater.getCategoryRegistryURL(), categoryController);
        Naming.rebind(ServerLocater.getBookActivityRegistryURL(), bookActivityController);
        System.out.println("Server is Ready.");
    } catch (NumberFormatException | MalformedURLException | RemoteException e) {
        System.out.println("Exception in Server.main: " + e);
    }
}

private static void startRegistry(int rmiPortNum) throws RemoteException {
    try {
        Registry registry = LocateRegistry.getRegistry(rmiPortNum);
        registry.list();
    } catch (RemoteException ex) {
        System.out.println("RMI registry is not located at port " + rmiPortNum);
        Registry registry = LocateRegistry.createRegistry(rmiPortNum);
        System.out.println("RMI registry created at port " + rmiPortNum);
    }
}

}

1 个答案:

答案 0 :(得分:0)

没有。您不必启动任何线程或实施任何import

您只需确保远程方法实现是线程安全的。