Multithreaded client problems
Newbie to the forum but i have a problem while trying to implement RMI client server. I have the client and server setup fine and i have read that RMI is already multithreaded to handle different clients. Again i have this running fine but i was wondering if anyone could give me some advice as to manually serve each client with a separate server thread, allowing me to control the threads when trying to access shared resources such as database etc. at the moment i think something to do with instatianting / constructing new classes that extend thread or implement runnable ?!?!
any help is appreciated and even directing
Message was edited by:
onlynew4now
# 1
Hi,
you do not have to generate new threads. The RMI mechanism does this by itself (it is necassary). You should concentrate on your remote methods: In the method you can create new objects. These created objects are exclusivly for each call of the remote method.
In comparision to a multi-threaded socket server: The method is like you are in a thread which you created for each new socket connection.
So: You do not have to care about multi-threading. But: if you access shared objects (like in a multi thread socket server), you have to synchronize methods.
People, please correct me if I am wrong.
Dak
null
# 2
thanks for the reply, i was under the impression that if 2 or more clients called the same remote method and created a new instance of a worker class, regardless of what 1 does it will not affect the other just the database. I know i could synchronize the method to handle 1 at a time but can i allow to separate remote calls to the same method for example without them affecting each other etc and how ?!
# 3
hi,I think: clients calling the same remote method are separated. if you create a new worker object in such a method the worker object is only for the caller.But: shared objects which you create e.g. in the Class as a private variable, exists only one time.
# 4
ok i think i might finally understand my problem for what i need. does each separate client call to the same remote method create a new instance of the method, if not does anyone know where i would do this or how ?!
# 5
I think it is "a kind of new method instance" for each call. But there is only one instance for the class which provides the remote method.
# 6
> I think it is "a kind of new method instance" for
> each call. But there is only one instance for the
> class which provides the remote method.
This answer just adds to the confusion. There is no such thing as a 'kind of new method instance'. There are threads.
(a) There are only as many instances of the remote object as you have created yourself.
(b) Method [invocations on each such object are generally dispatched in different threads, at least one per client.
The RMI specification is slightly confusing on this question but the one certainty from what it says is that you can't rely on remote method despatching model being single-threaded: you have to assume multi-threading and take appropriate precautions about syncrhonization and database parallelism yoursellf.
ejpa at 2007-7-12 10:06:16 >

# 7
I also have similar problems.If each client call is a different thread, how would I create an instance of a remote method so that only that thread will affect the variables for the remote method ?noob.
# 8
Hello,
> > There is no such thing as a 'kind of new method instance'."a kind of new method instance"
yes, therefore I used quotes. Each method-call is dispatched. Therefore you can create new objects in the method-body so that other clients do not have access to exactly these objects you created "localy" in the method-body for one client.
That's right, isn't it?
# 9
as with regards to creating a new instance, how can / is it possible for the client call thread to create a new instance of the remote method / class then act as the as that service thread. Sorry if this is confusing what im trying to get at is how can i manage separate clients on different service threads.
i think for using sockets its something like :
(new Thread(new myobject(asocketconnection))).start();
also if i try to manage threads in a remote method, how can i return values back out of the remote method if a thread create there starts from the public void run() as its return type must be void ?!
any help is grateful
# 10
> That's right, isn't it?That is right, but it has nothing to do with RMI or threads or anything mystical called a 'kind of new method instance'. It's just how local variables work.
ejpa at 2007-7-12 10:06:16 >

# 11
> Sorry if this is
> confusing what im trying to get at is how can i
> manage separate clients on different service threads.
You don't have to worry about 'service threads'. RMI does that for you. As for managing different clients, consider this:
interface Login implements Remote
{
// Singleton login
Session login(String userid, String password) throws RemoteException;
}
interface Session implements Remote
{
// per-client methods
// your RMI methods here
}
class LoginImpl extends UnicastRemoteObject implements Login
{
// ctor &c omitted
public Session login(String userid, String password) throws RemoteException
{
// validate userid/password, not show, throw some exception if
// invalid
return new SessionImpl(userid);
}
}
public class SessionImpl extends UnicastRemoteObject implements Session
{
// your per-client remote method implementations in here
}
> also if i try to manage threads in a remote method
You don't have to do that. Just use synchronized blocks where you need to, or java.util.concurrent data structures.
> how can i return values back out of the remote method
> if a thread create there starts from the public void
> run() as its return type must be void ?!
The question makes no sense but as you don't create threads in remote methods anyway it has no application.
ejpa at 2007-7-12 10:06:16 >

# 12
> Newbie to the forum but i have a problem while trying
> to implement RMI client server. I have the client and
> server setup fine and i have read that RMI is already
> multithreaded to handle different clients. Again i
> have this running fine but i was wondering if anyone
> could give me some advice as to manually serve each
> client with a separate server thread, allowing me to
> control the threads when trying to access shared
> resources such as database etc. at the moment i think
> something to do with instatianting / constructing new
> classes that extend thread or implement runnable
> ?!?!
>
> any help is appreciated and even directing
>
> Message was edited by:
> onlynew4now
RMI can handle different clients simultaneously. Whether your server can or not depends on how you designed your server.
Are you asking about a multithreaded client, or multiple single threaded clients accessing the same RMI server?
# 13
yup thats it multiple single threaded clients accessing the same RMI server which controls access to a database. il have to handle concurrency issues but i was under the impression i could spawn a new thread for each client.
# 14
Wherever you got that impression you were misinformed. RMI does that for you.
ejpa at 2007-7-12 10:06:16 >
