• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about RMI & Thread

 
Jofer Chan
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,everyone:
In Server,I use RMI,the Server.java codes follow:

the RemoteDataAccessFactoryInterface interface receive RemoteDataAccessImplement class,the RemoteDataAccessImplement class is a adapter,it implement RemoteDataAccessInterface and extends DBAccess interface,the Data class also implements DBAccess interface.
the above is their relation.
when the Server class is running,the MultiClient can connection the Server at one time,Why use MultiThread?
if use MultiThread,How to implement MultiThread of my Server class ?
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jofer,
the RemoteDataAccessFactoryInterface interface receive RemoteDataAccessImplement class,the RemoteDataAccessImplement class is a adapter,it implement RemoteDataAccessInterface and extends DBAccess interface,the Data class also implements DBAccess interface.

Sorry, just trying to get a bit of clarification here...
Does the RemoteDataAccessFactoryImplement class create instances of the RemoteDataAccessImplement class?
when the Server class is running,the MultiClient can connection the Server at one time,Why use MultiThread?

Are you talking about a class named MultiThread? Or are you talking about multi threading as a concept.
The good thing about RMI is that it creates multiple threads for you.
The bad thing about RMI is that it creates multiple threads for you.

Simply because you are using RMI, your code instantly can have multiple threads running on it. Therefore you must make your code thread safe.
Consider the following code:

When I ran it, this is the output I got:

Note that I deliberately started and identified 4 client threads. RMI started (at least) 4 server threads - all running on the single instance of the Server() class.
And as you can (hopefully) see, the instance variable counter is not thread safe and is not being used in thread safe classes - clients 1 & 3 found a different value in counter at the end of their execution of the server code than when they started.
Regards, Andrew
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic