public class Testprivate staitc Object obj;
public void xxx()
{
XxxHome home = EJBHomeFactory.getInstance().getHome("");
XxxObject obj1 = home.create();
XxxObject obj2 = obj1.xxxMethod();
obj = obj1;
IRemoteObject obj = (IAppContextServiceRemote)UnicastRemoteObject.exportObject(new RemoteObj(),0);
registry.rebind("myRemoteServcie", obj);
}由于匿名对象创建后,没有其他的引用,导致方法返回后,可能会被system GC(即JAVA的垃圾回收),时好时坏的现象所以我现在想把obj1 和 new RemoteObj()都跟一个static的对象绑定。以避免被gc回收。
但是我不知道应该怎么绑定。应为该环境是并发的多线程环境。所以如果第一个new RemoteObj()我给绑定到一个static对象。
那并发的时候,static对象又给绑定到第二个new RemoteObj()上了,第一个又面临被gc回收的境地了啊。不知道应该怎么写才可以在并发的时候也保证不让obj1 和 new RemoteObj()不被gc回收呢?
相关的问题,说的很清楚啊:就是跟这个问题差不多啊!
http://blog.sina.com.cn/s/blog_58c679ff0100a23m.html
【
1)起因:使用了一个rmi服务,奇怪的是,同一个RMI服务器export的多个远程对象有的能访问,有的不能,而且还不一定能,即:有的时候能,有的时候不能。异常如下: java.rmi.NoSuchObjectException: no such object in table
java.rmi.NoSuchObjectException: no such object in table
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:126)
at com.domain.Monitor_Stub.accept(Unknown Source)
at com.domain.Scheduler.Server.jobs.VerifyFailedMonitorsJob.execute(VerifyFailedMonitorsJob.java:60)
at org.quartz.core.JobRunShell.run(JobRunShell.java:195)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)
the code to perform lookup and then some operations looks like this:2)分析:经过分析,发现由于在远程服务器export远程对象的时候是匿名对象的方式: IRemoteObject obj = (IAppContextServiceRemote)UnicastRemoteObject.exportObject(new RemoteObj(),0); registry.rebind("myRemoteServcie", obj); 关键点在于:匿名对象创建后,没有其他的引用,导致方法返回后,可能会被system GC(即JAVA的垃圾回收),所以出现了可能可不能的现象。 3)解决方法:创建static静态变量,对RemoteObj进行引用。这样即使方法返回,也不会没有对此对象的引用。】
public void xxx()
{
XxxHome home = EJBHomeFactory.getInstance().getHome("");
XxxObject obj1 = home.create();
XxxObject obj2 = obj1.xxxMethod();
obj = obj1;
IRemoteObject obj = (IAppContextServiceRemote)UnicastRemoteObject.exportObject(new RemoteObj(),0);
registry.rebind("myRemoteServcie", obj);
}由于匿名对象创建后,没有其他的引用,导致方法返回后,可能会被system GC(即JAVA的垃圾回收),时好时坏的现象所以我现在想把obj1 和 new RemoteObj()都跟一个static的对象绑定。以避免被gc回收。
但是我不知道应该怎么绑定。应为该环境是并发的多线程环境。所以如果第一个new RemoteObj()我给绑定到一个static对象。
那并发的时候,static对象又给绑定到第二个new RemoteObj()上了,第一个又面临被gc回收的境地了啊。不知道应该怎么写才可以在并发的时候也保证不让obj1 和 new RemoteObj()不被gc回收呢?
相关的问题,说的很清楚啊:就是跟这个问题差不多啊!
http://blog.sina.com.cn/s/blog_58c679ff0100a23m.html
【
1)起因:使用了一个rmi服务,奇怪的是,同一个RMI服务器export的多个远程对象有的能访问,有的不能,而且还不一定能,即:有的时候能,有的时候不能。异常如下: java.rmi.NoSuchObjectException: no such object in table
java.rmi.NoSuchObjectException: no such object in table
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:247)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:223)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:126)
at com.domain.Monitor_Stub.accept(Unknown Source)
at com.domain.Scheduler.Server.jobs.VerifyFailedMonitorsJob.execute(VerifyFailedMonitorsJob.java:60)
at org.quartz.core.JobRunShell.run(JobRunShell.java:195)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)
the code to perform lookup and then some operations looks like this:2)分析:经过分析,发现由于在远程服务器export远程对象的时候是匿名对象的方式: IRemoteObject obj = (IAppContextServiceRemote)UnicastRemoteObject.exportObject(new RemoteObj(),0); registry.rebind("myRemoteServcie", obj); 关键点在于:匿名对象创建后,没有其他的引用,导致方法返回后,可能会被system GC(即JAVA的垃圾回收),所以出现了可能可不能的现象。 3)解决方法:创建static静态变量,对RemoteObj进行引用。这样即使方法返回,也不会没有对此对象的引用。】
那thread2新一个这个对象又指向那个static的对象。这样thread1的那个对象不是又会被gc回收了吗?
IRemoteObject obj = (IAppContextServiceRemote)UnicastRemoteObject.exportObject(new RemoteObj(),0); 把new RemoteObj() 用一个static变量引用起来private static Object obj6 = new RemoteObj();IRemoteObject obj = (IAppContextServiceRemote)UnicastRemoteObject.exportObject(obj6 ,0); 这样试一试