• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

writeRecord() in DataClass synchronized ?

 
Vivian Richards
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Isn't writeRecord() in Data class need to be synchronized. ?
This is because:
1. lets say, a thread T1 wants to Add a new record
2. at the same time, thread T2 wants to modify record number 1.
Both add() and modify() methods call writeRecord() which is not synchronized.
This will result in trying to write to the file at the same time at two different places and it could corrupt the file itself.
Please let me know whether there is anything wrong in my assumption and what you think about making the writeRecord() synchronized.
 
Narayan Veeramani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the scenario suggested by you cannot arise.
Once thread T1 is in the synchronized add() method, it has already locked the object reference (this).
So no other thread (in this case T2) can start executing another
synchronized method (in this case modify()) unless thread T1
finishes executing the synchronized method add().
So in a nutshell, I think writeRecord() need not be synchronized
for this scenario (although it does not hurt to make the method
synchronized and thread safe for other scenarios).
 
Narayan Veeramani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the above discussion, I did assume that the Data Object
is a singleton object.
[This message has been edited by Narayan Veeramani (edited June 29, 2001).]
 
Thomas Mathai
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that writeRecord() is private. So as Narayan pointed out, it can only be called through some of the public methods like add, modify which are synchronized. So writeRecord() need NOT be synchronized. The same argument will apply to readRecord(). But I see that readRecord() is synchronized! I don't see why it should be synchronized either.
 
Narayan Veeramani
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting observation, Thomas.
It is bizzare that the readRecord() method is synchronized while
the writeRecord() has not been declared synchronized.
Further within the criteriaFind() method,
I am calling the seek() method to loop through the records in the file. Is it required to make this method synchronized as well for preventing the file pointer from
getting messed up by multiple threads?

[This message has been edited by Narayan Veeramani (edited July 01, 2001).]
 
Kevin Yip
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about synchronize writeRecord() rather than add() and modify()? Look like this will improve performance as writeRecord() is the only code block that causes data corruption.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic