Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EOF Exception while inserting record through threading

 
pramod karnani
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

As you know Roberto Perillo made test class to test locking mechanism

http://www.coderanch.com/t/427863/Developer-Certification-SCJD/certification/Tests-Data-class-locking-mechanism


when I test my code while insert new record for e.g insert 10 new records the way implemented in above url ,I get EOF Exception in readContractor method at line that is bold in the below code snippet:


RECORD_SIZE=182
final byte[] input = new byte[RECORD_SIZE];
Contractor contractor = new Contractor();

synchronized (database) {
database.seek(locationInFile);
/*
* read the deleted flag value
*/
int deletedFlag =database.readUnsignedByte();
contractor.setDeletedFlag(Byte.parseByte(Integer.toString(deletedFlag)));
//contractor.setDeletedFlag(input[0]);
database.readFully(input);
}


Am I reading the deleted flag in correct way ?

In my assignment given that

"1 byte "deleted" flag. 0 implies valid record, 1 implies deleted record "


I am reading all the record of the database file in for loop in another method that call the readContractor

where RECORD__LENGTH =183
for (long locationInFile = FIRST_RECORD_POSITION; locationInFile < database.length(); locationInFile += RECORD_LENGTH) {
Contractor contractor = readContractor (locationInFile, recNo);

what I did wrong?


but when I tried to insert only one record with the CreateRecordThread that is given in above URL then it works fine .


It only brake my code when I do this


for (int i = 0; i < 10; i++) {
Thread creatingRecord = new CreatingRecordThread();
creatingRecord.start();


}

if i counter will run only one time then this will not brake my code but if counter will run more than one I am getting EOF Exception at line
int deletedFlag =database.readUnsignedByte();


something is wrong the way i read deleted flag?







 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you get EOF Exception somewhere, that means you calculated the record number wrong some where. Check you record number generation. Then check your data file if there are extra square symbols in between each record.

I came across this and my problem was the record number generation.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly. All the tests do is call the methods from the public API of the interface provided by Sun concurrently. Please take a look and verify if you coded everything correctly.
 
Anne Crace
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had to play around with what I sent in to that test class to get it to work properly. For instance, I included the valid flag in mine, so I had to send in something equivalent to 2 bytes. It's a great test but does need to be modified for individual users. Good luck
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anne Crace wrote:It's a great test but does need to be modified for individual users.


Exactly! Here's a piece of the post:

Some customization of this code will be necessary, according to the implementation of each developer.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic