jQuery in Action, 2nd edition*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How many instances of remote object should be built in multi-client system? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "How many instances of remote object should be built in multi-client system?" Watch "How many instances of remote object should be built in multi-client system?" New topic
Author

How many instances of remote object should be built in multi-client system?

Along huang
Ranch Hand

Joined: Feb 09, 2004
Posts: 72
I use RMI to implement comunication in my assignment,but I find a problem.

Assume here is a remote object named DataMgr, the sample code as below:

String url = "localhost/DataMgr";
DataMgr dataMgr = new DataMgrImpl();
Naming.rebind(url,dataMgr);

As above,I have only created a instance of DataMgr, if many clients access the remote object simultaneously, what is happen? whether the server build a new instance of DataMgr for client's request automatically?

Or should do I create many instances at initial state of server?

thanks


SCJP1.4 SCJD 2<br />SCEA(In progressing)
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
hi Along:
I am not an expert on RMI, but I will explain this according to my understanding. When a client ask for an object from the server, the server returns a sub, which represents the object. The server doesn't create any new objects, and you don't need to creat any new objects either. If the remote object does any IO operation, and you want to make it thread safe, then you must implement synchronization mechanism to ensure data intigrity.


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11285
    
  59

Hi Along,

In the case you described, there will only ever be one instance of DataMgrImpl, and (as Hanna mentioned) there may be multiple threads running on this one instance, so you will need to make it thread safe.

This may be sufficient for your needs if your Data class uses cookies and/or you are using thin client bookings.

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Along huang
Ranch Hand

Joined: Feb 09, 2004
Posts: 72
Thank you for response.

I have more a question about thread, a remote method below:

public int calc(int initialization){
initialization = initialization / 10;
int count = 100 + initialization;
return count;
}

Assuming here are two clients invoking the remote method at a same time:
1). System.out.println(xxx.calc(20));
2). System.out.println(xxx.calc(50));

The method is not set as Synchronized, whether each of the two remote clients can return the correct value?

I know if two threads share a resource outside method, confusion will occur. But in this case showed above,the resource(variable "initialization") is inside method.

Substantively, I think that concern the assignment of memory,i.e.,when many thread access a method of a object simultanenously, system assign a memory for each thread, or only a menory for all thread?

regards
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
hi huang :
I don't know what you mean by the system will assign memory to each client. When a thread invoke a method, a stack will be created and each thread will get its own stack and its own version of the local variabls. If the method doesn't use any external parameters (i.e instance varaibles) ro share any external resources (i.e. db file), then it is thread safe.
[ June 18, 2004: Message edited by: Hanna Habashy ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many instances of remote object should be built in multi-client system?
 
Similar Threads
share one instance of remote object
[NX] Adapter problem
Final review - PLEASE READ
RMI and object and thread sharing
URLyBird - Question about Singleton pattern