• 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

SCJD Book - locking question

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is a code fragment of sample application from the "SCJD Exam with J2SE 5": class DvdFileAccess, method persistDvd.


The actual write to the RAF takes place after this code fragment.

Suppose 2 threads both want to create a new record.
1. thread 1: get writelock.
2. thread 1: get offset: offset = database.length();
3. thread 1: release writelock.
4. thread 2: get writelock.
5. thread 2: get offset: offset = database.length();
6. thread 2: release writelock.
7. thread 1: physical write.
8. thread 2: physical write.

At 5. no physical write has taken place yet, the database length is unchanged, and the value of the offset will be the same as at 2.
At 8. the new record ot thread 2 will now overwite the new record of thread 1.

Any comments ?


Regards,
Peter Joosten.
 
Peter Joosten
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Same topic as:
"Synchronization/locking question on persistDVD in Andrew's book - Daniel Bryant". Continue discussion there. This topic can be closed.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The actual write to the RAF takes place after this code fragment.

Suppose 2 threads both want to create a new record.
1. thread 1: get writelock.
2. thread 1: get offset: offset = database.length();
3. thread 1: release writelock.
4. thread 2: get writelock.
5. thread 2: get offset: offset = database.length();
6. thread 2: release writelock.
7. thread 1: physical write.
8. thread 2: physical write.

At 5. no physical write has taken place yet, the database length is unchanged, and the value of the offset will be the same as at 2.
At 8. the new record ot thread 2 will now overwite the new record of thread 1.

Any comments ?



think you're right...
The write lock should be released after the record has been written to the database. Then, in above example, your thread 2 will obtain the write lock only after
thread 1: physical write
and now thread 2's offset should be correct.

Chulwoo
 
We're being followed by intergalactic spies! Quick! Take this tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic