2013-06-26 37 views
0

我的應用程序使用rmi服務客戶端請求來操縱數據庫上的數據(使用JDBC)。我希望骨架爲每個客戶端的操作請求運行一個線程。我只需要像RMI多線程框架

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

還是別的什麼?

+0

絕對不是'run()'。線程以'Thread#start()'開始。 –

+0

是的,只是混淆 – giozh

+0

好了,現在你的線程將啓動並執行你在其中覆蓋的'run'方法,或者你在其構造函數中傳遞的'Runnable'中的'run'方法。 –

回答

3

我想那骨架運行每個客戶的操作線程請求。

RMI已經做到了。

我只需要對類似

不,你不會。只需正常寫入方法即可。 RMI將爲您多線程。

+1

+1一如既往的經典和晶瑩剔透回答。 –

4

您不需要做任何特別的事情,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(); 
    } 
}