• 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!