This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
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
Joined: Nov 22, 2003
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
Joined: Dec 25, 2003
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.
author and jackaroo
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
Joined: Dec 25, 2003
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!
author and jackaroo
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 ]