aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes rmi- a new Obj or new thread is created for each client? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "rmi- a new Obj or new thread is created for each client?" Watch "rmi- a new Obj or new thread is created for each client?" New topic
Author

rmi- a new Obj or new thread is created for each client?

Peter Yunguang Qiu
Ranch Hand

Joined: Nov 22, 2003
Posts: 99
I wonder how rmi works? A new RmiImpl's Object is created for each client? Or a new thread is created for each client(only one instance of rmiImpl)? Or works other way?
Peter
Anurag Mishra
Ranch Hand

Joined: Sep 27, 2001
Posts: 133
Hi,
RMI Server is by-default a Multithreaded server. For each client, it creates a new thread i.e. only one instance of rmiImpl. As in case of Sockets, the developer has to explicitely create a seperate thread for each client. Hope this help.
Anurag Mishra
SCJP,SCJD
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11508
    
  95

Hi Peter & Anurag,
Originally posted by Anurag Mishra:
For each client, it creates a new thread i.e. only one instance of rmiImpl.

It may create a new thread, or it may use an existing thread - there are no guarantees.
Regards, Andrew


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

Joined: Dec 25, 2003
Posts: 160
Just a quick question on RMI, in another words, am I right to say that in the RMI implementation of the assignment, there is no way to track how many clients are connected to the server?
If there is a way, how to do it?
Peter Yunguang Qiu
Ranch Hand

Joined: Nov 22, 2003
Posts: 99
Hi, Anurag and Andrew:
thaks for help.
Both of you agree that only one instance of rmiImpl but multithreads of it, right?
What is the difference of a thread and an instance of an object? I think a thread is an object's instance that is executing. This object extends Thread or implements Runnable so it has a run() method. In a multithreads' socket server, when a client's request comes, the server do this:The server creates a new thread for each client. The new thread is a new instance of ObjectWithSocket. So, what is the difference of new instance and new thread? I think they are ONE thing but have different names in different context.
For example, Bill is a CEO, is a father and husband, is a son. In the context of his company, he is a CEO. In the context of his family, he is a father and husband. In the context of his parents' family, he is a son.
Rmi's implementation is based on socket but hidden to programmers.
What is the differnce and effect of multi threads of one instance and multi instances? Anyone has a good idea and good explanation?
Peter
Xie Ruchang
Ranch Hand

Joined: Dec 25, 2003
Posts: 160
Hi Peter,
Think I can help on help.
There is no direct relationship between a thread and an instance.
A thread is a line of execution of java codes, it can span one instance or across many instances. It may even exist without an instance of an object. For example, consider the following program.

When you run this program by the command java Helloworld, a main thread is created by the JVM to execute this program, this is no object instance of Helloworld at all.
To create an additional thread in Helloworld, we can do this,

Now, on top of the main thread, you have another thread running with an instance of an anonymous class. The output of the above depending on the JVM may look something like this.
Line A
Line 1
Line B
Line 2
Line C
Line 3
So there are two lines of execution. The program ends when these two threads terminate.
Suppose, I change the program now to

The above code will run displaying a message box, after clicking OK, the program will have the same output, BUT the program doesn't terminate after displaying the output. The reason is because there is still an outstanding thread running. There are altogether THREE threads in this example as contrast with TWO threads in the previous. The one additional thread was created by the JVM to do the Swing stuff, ie the message box. The main program will only ends when all the thread end. In this case, there is one outstanding which is the Swing thread, somewhere behind the scene it is doing a looping to handle swing events. To end this thread, we could use System.exit(0);

So in the above example, we see three lines of execution in one program. Hope this will help in understanding threads.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11508
    
  95

Hi Frankie,
Originally posted by Frankie Cha:
Just a quick question on RMI, in another words, am I right to say that in the RMI implementation of the assignment, there is no way to track how many clients are connected to the server?
If there is a way, how to do it?

I don't think you can do it if you just use a multi threaded single instance of the remote code.
However you can use a connection factory on your server side, which will create a unique instance of a connection object for each connected client. You then have at least two potential ways of tracking how many connected clients exist:
  • Create a counter for connected clients. When a client requests a remote object through your connection factory, increment the counter. Implement Unreferenced in your connection object. When unreferenced is called, perform any cleanup necessary, and decrement your counter.
  • When you create your connection object, store a WeakReference to it in a collection. Then whenever you want to know how many clients are connected, just check how many items are in the connection.


  • Note that both methods rely on the distributed garbage collector cleaning up after a client disconnects. This means you may be waiting up to 20 minutes for the number to decrement after a client crashes.
    Regards, Andrew
    Xie Ruchang
    Ranch Hand

    Joined: Dec 25, 2003
    Posts: 160
    Thanks Andrew, I think it is not a good method as you mentioned about the time delay. In other words, the Socket implementation would be superior to RMI if and only if we want to keep track of how many clients are there.
    Best Regards!
    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11508
        
      95

    Hi Peter,
    Originally posted by Peter Yunguang Qiu:
    What is the difference of a thread and an instance of an object?

    One thing I think Frankie did not point out clearly was that having multiple threads operating on a single instance of a class is by default not thread safe.
    Here's some code snippets to explain this.
    First having multiple threads operating on a single instance:

    When I ran this, I got the following output:

    As you can see, it is not thread safe: thread 0 started with counter = 4 but by the time it completed, the counter was = 5.
    Now for a multi instance example:

    When I ran this, I got the output:

    All nice and thread safe!
    Regards, Andrew
    [ January 05, 2004: Message edited by: Andrew Monkhouse ]
    Peter Yunguang Qiu
    Ranch Hand

    Joined: Nov 22, 2003
    Posts: 99
    Thanks Andrew and Frankie.
    Peter
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: rmi- a new Obj or new thread is created for each client?