• 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Concurrency problem in URLyBird

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Data.java puzzled me. My Data class extends java.io.RandomAccessFile and is not singleton pattern. All methods marked synchronized on static variable. Every record has a Read-Lock and Update-Lock. If a record is locked on its Update-Lock, no one can read and update it, and if it is locked on its Read-Lock, other threads can read but not update it. I use the lockCookie as the Update-Lock. In order to implement, I have a complex synchronized mechanism by using two java.util.concurrent.ConcurrentHashMap to store the locks for every record.
Is it necessary to be so complex?
Do I comprehend the instructions.html and the interface (DBAccess) wrongly?
 
Bartender
Posts: 3648
16
Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Xin,

Your Data class extends java.io.RandomAccessFile? Or just use it? About your approach, it looks like you are using the read-writelock design pattern with those read-lock/update-lock flags. Honestly, your current approach is a bit complex IMO. Do you really need 2 ConcurrentHashMaps?

When I first did my locking mechanism, I also tried using the read/write lock design pattern and sort of come across what you are expecting - some where there are records waiting meaning deadlock. The contention is really comes from when to set those flags on/off. However, this design pattern is indeed easy to understand yet quite hard to get it perfect.

Then I created my own class for locking using some singleton collection (ie only one record can be present in the collection) to control what record is currently locked. Now thinking back on this, I could have simplified this approach a bit more from what I have done.

Anyway hope I shed some light.
 
Xin Gang Sun
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, K. Tsang.
My Data extends java.io.RandomAccessFile and implements DBAccess. In my program, every thread has a Data instance to access data file.
You are right, it looks like the read/write lock design pattern. ConcurrentHashMap is necessary because it is atomic, it is important in my methods in order to ensure the locks thread-safe.
Now I have finished it, though it is hard to debug.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Xin,

Like K. Tsang already pointed out: I don't think extending from RAF is a good approach, actually it is the first time I hear someone extending from RAF.

This would be a more appropriate approach:


Kind regards,
Roel
 
Xin Gang Sun
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, K. Tsang and Roel.
You are right. It is not logical.
I modified my design.
 
K. Tsang
Bartender
Posts: 3648
16
Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Xin, if you are using RAF then why you need a separate class that extends RAF? Roel and I were saying more like:


About your RandomAccessDatabaseFile ... more of an interface from the code you provided.
 
Xin Gang Sun
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, K. Tsang.
Yes, but I think two designs are the same. My RADF class extends RAF and add readRecord() and writeRecord() method. In Data methods, readRecord()(a method in my RADF) instead of readXxx() can be more clear in my code. Actually, they are the same.
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But would you want your Data class calling the java.io.RandomAccessFile#write(byte[]) method of your RADF class directly? It is possible if you extend RAF.
 
Xin Gang Sun
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it can call all of the methods. I think that making a RADF class is more Object-Oriented. But it is unnecessary actually.
 
Maybe he went home and went to bed. And took this tiny ad with him:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic