Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tricky question about DataServer Thread safe

 
Wai Iu
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exam document mentioned that we should provide a thread-safe DataServer implementation. I have a question about this issue:
My code:

The above code says that everytime a user tries to request a reference from DataServer, a new instance of Data class will be created for each user. Since the Data class has lock(), unlock() methods(guaranty only one user can update a record at the same time if there are two users try to update the same records) and syncronized methods(guaranty that only one thread can be executed at one time if there are three threads try to assess the db.db file at the same time). I think that my code will be thread safe.
However, I saw some previous dicussions about this issue here. some suggest that when implementing a remote interface(DataServerInterface in this case), the implementation method should apply a Singleton design and only generate one instance of Data class. This means that no matter how many users try to assess db.db fiel, only one instance of Data class will be created.
I am not sure the difference between my code and the Singleton design. Actually, all these come to one question: Does every instance of Data class has its own monitor? Or all instances of Data class have only one monitor? If 1st case, my code is wrong since mutile instances of Data class can assess their own synchronized method at the same time. This definitly is not thread-safe. If 2nd case, my code is right since all instances of Data class will compete for one monitor. This lead to thread safe.
Please help me clarify this? Thanks!

 
Trevor Dunn
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the same thoughts also. However I rationalized that if I create my instance of Data on server startup then only one instance will ever be bound to the registry, unless of course you start multiple servers.
This may not be correct and I would also like to hear others opinions on this matter
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wai Iu:
The above code says that everytime a user tries to request a reference from DataServer, a new instance of Data class will be created for each user.

A recipe for disaster! You cannot safely let multiple Data classes operate on the same file. For a start, take another look at the Data instance variables.
some suggest that when implementing a remote interface(DataServerInterface in this case), the implementation method should apply a Singleton design

A misconceived suggestion. Singleton is one of the most abused design patterns, and Data should certainly not be a Singleton. For a start, it is quite acceptable for a database to have multiple tables...
This means that no matter how many users try to assess db.db fiel, only one instance of Data class will be created.

There is no other way.
Actually, all these come to one question: Does every instance of Data class has its own monitor? Or all instances of Data class have only one monitor?

Tut tut, this is SCJP material Every instance of a class has its own monitor lock. For class-wide locking, lock the class object itself (Data.class). But monitors are not the issue here. Multiple Data instances against the same file simply won't work.
- Peter
 
Wai Iu
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter:
Thanks for your advice. This thread safe issue really annoys me. I carefully read your reply:
1 You relpy:

You cannot safely let multiple Data classes operate on the
same file.
[/qoute]
I totally agree.
2. You reply:

A misconceived suggestion. Singleton is one of the most
abused design patterns, and Data should certainly not be
a Singleton. For a start, it is quite acceptable for a
database to have multiple tables...

I am confusing about this. What I originally mean is to generate one instance of Data class from DataServer class by using Singleton pattern. Is this wrong? Data class itself is just an onject, not a Singleton. Singleton is just a pattern name, which generate only one instance of the certain object by using it. Can you clarify it a little more?

By the way, my Dataserver design is to use Data server as a proxy for the Data class. could you give some ideas about this structure of the Data Server? Now I am a little confusing?
Thanks a lot!
 
Kevin Yip
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wai, Singleton is used when you need to make sure that there is only ever ONE instance of a class.
Imagine that the Fly By Night company later wants to add/use a new table, say, Customer that contains all information about their customers. If Singleton pattern is implemented, you'll not be allowed to create/use a new table (say,using new Data("cust.db")) and you have to write new codes to go around this restriction. This is rather undesirable in terms of code reusability and for no special reason.
We need to avoid creating a new instance of the Data object which encapsulates the Flight information (the file "db.db") for each additional user because every user should access the same database all the time. However, we are NOT to avoid creating a new instance of the Data object for another table which encapsulates the customer info (the file "cust.db"), which is, of course, again only one instance should be created.
Make it short and sweet, we cannot use new Data("db.db") more than one time, but we should be able to use new Data("cust.db"), new Data("employee.db"), new Data("whatever.db")... as much as you wish.
[This message has been edited by Kevin Yip (edited July 16, 2001).]
 
Trevor Dunn
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have question. If I create an instance of my RemoteDataAdapter class and bind that to my registry, and this is only ever done once, does it solve the problem without the singleton pattern.
Because I only have one instance of RemoteDataAdapter available in the registry to service multiple clients. So make it threadsafe and all is well. As a result I can still bind other instances of Data with different db.db files

Trevor
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trevor, that sounds reasonable to me.
- Peter
 
Trevor Dunn
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter
Trevor
 
Wai Iu
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Peter, Kevin, Trevor. It really helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic