Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

URLyBird 1.3.3 Locking question

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all:
here is my question...

public class Data exntends UnicastRemoteObject implements DBMain {
public synchronized String[] read(int recNo) throws
RecordNotFoundException, RemoteException {
//implement
}
public synchronized void update(int recNo, String[] data) throws
RecordNotFoundException, RemoteException {
//implement
}
public synchronized void delete(int recNo) throws
RecordNotFoundException, RemoteException {
//implement
}

// other method implementation ......
}

Every client will get the same Data object,
if ClientA wants to delete(13), there is no need to lock(13) first,
because ClientA must had object lock first and then can execute delete(13), in the situation, which client have data object lock, and the client is the only thread that allowd to access the database.
why still need lock(), unLock(), isLock() mechanism?

hopes my question is clearly ^^"
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If every method in your Singleton style Data class is synchronized, then simultaneous reads and writes are not possible. Many programmers want to support at least simultaneous reads, so they don't synchronize every method of the Data class and make it a Singleton.

You are right though - if you implement the Data class this way, there is no need for locking/unlocking on a record level.
 
Ching-Tien Chang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you Mr.Anthony, this is the answer that want ^^

another question:
please reference the thread:
https://coderanch.com/t/183457/java-developer-SCJD/certification/NX-URLyBird-single-remote-object

the topics discuss about "single remote object or multi-remote object?"
thought many people reply for this topic, but still no answer about
"why not just use one single remote data class for each client and any disadvantage??"

could anybody give me answer? thanks vary much.
 
Anthony Watson
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a single Data instance that has a RandomAccessFile as a data member, then you would have to address the thread safety issues that occur when multiple threads are trying to move the RandomAccessFile's pointer around. With multiple Data instances, you do not have that problem.
 
Ching-Tien Chang
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr.Anthony:
Thanks for your kindly answer,
I will pay more attention on my locking mechanism.

Sincerely
Ching-Tien Chang
 
    Bookmark Topic Watch Topic
  • New Topic