• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

question about RMI & Thread

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic