• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJD Book - locking question

 
Peter Joosten
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.
 
Chulwoo Choi
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic