Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Locking question - URLyBird 1.2.1

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone, I've been reading the forum for a while and just started my SCJD assignment. I'm working on URLyBird 1.2.1.

First question from me, the specification states:

You may assume that at any moment, at most one program is accessing the database file; therefore your locking system only needs to be concerned with multiple concurrent clients of your server.



Does that mean shared read lock is not allowed? i.e. there will be no concurrent read? I was going to use ReadWriteLock class for locking, but it seems synchronized block is sufficient since shared read lock is not required?

Appreciate your thoughts.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are planning to use RandomAccessFile for reading/writing data, then reading/writing operations should be in synchronized context.
Imagine a scenario when two threads are trying to read from different locations of the data file - one gets pointer to read from one location and then the other moves the pointer away to read from another location.
 
Ericsson Liu
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I am using RandomAccessFile. Does that mean i do not need to worry about locking on read/write to the db file since RandomAccessFile takes care of it?
 
Jevgeni Zhukov
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ericsson Liu:
Does that mean i do not need to worry about locking on read/write to the db file since RandomAccessFile takes care of it?


No, it means that you do need to synchronize on those operations so that only one thread can read/write at a time.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both the read and write options executed by a RAF should be done in a synchronized context.
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
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
reply
    Bookmark Topic Watch Topic
  • New Topic