【发布时间】: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