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?
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.
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.
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:
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.
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!
[ January 05, 2004: Message edited by: Andrew Monkhouse ]