我想知道如何在RMI中使用线程。例如,如何使以下Java简单代码变成多线程?
接口:
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已经多线程了。
发布于 2014-04-28 10:22:25
我听说RMI已经多线程了。
你是对的。不需要对此代码进行更改。一般来说,您需要确保远程对象的线程安全,但是这个特定的对象已经是线程安全的了。
https://stackoverflow.com/questions/23338233
复制相似问题