Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to make create() allows multi-thread

 
Yan Zhou
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is about B&S assignment, but should apply to all others.

How to make create() accepts multi-thread and provide sufficient locking? It can be easily implemented for update() to allow multiple threads as long as they do not update the same record. But with create(), it supposes to reuse a deleted record or create a new one, I could not think of a solution that allows multiple threads to get multiple record numbers at the same time.

If I simply synchronize create() method, that defeats the whole purpose of accepting multiple clients.
Any suggestions?

Yan
 
Yan Zhou
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thinking it over, I am not sure it is correct to try to get create() do multi-threading.

If there is no removed record, new record should start at the end of file. Further suppose there are 10 records in the file.

If there are multiple threads appending records at the end of file, they will get new record number, say 11 and 12. However, without the first thread finishing writing 11, the second thread cannot write 12 simply because the nature of file I/O.

Am I making sense?
Yan
 
Hu Yong Jun
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The creation does not need a record lock because there is no record it can lock, but it does need a object lock because it modifies the database.

One thread wants to create a new record and gets the next available recNo, says #29, at the same time the second thread gets the next available recNo, unfortunately #29 too. The first thread completes the creation and then the second thread throws duplicate key exception. The second can try to get next available recNo again.
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

throw new UnsupportedOperationException() :-)

Like all ppl who did that (that I know of) I got a good score (24/40 for the GUI, 100 % for the rest). But make sure that your assignment allows this. In my assignment I could do so for delete and create.

Conan
 
James Turner
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

How would it be possible to allow for multi-threaded reads/updated of different records at the same time?

The database would need to maintain a group of connections (streams) to the database. What would be the best way to implement that? List of RandomAccessFile streams? Assigning one to a requesting thread.

Any comments or thoughts?

Thanx

James.
 
James Turner
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nio provides things called channels that are basically io connections which would allow multiple threads to concurrently access various places of a file at the same time.

Does anyone know any way of mimicing this behaviour without nio? For example a collection of RandomAccessFiles, the total number of streams permitable could be controled from a startup properties file when the server is started.

This would make concurrent access to different records possible.

Any comments on this? Anyone know a better way of doing it?

Thanx for your advice,

James.
 
Yan Zhou
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not think the assignment expects us to allow multi-thread to access the physical file at the same time. It just wants the server to accept multi-thread that update different record numbers and when it comes to update the file, it has to be serialized since JD test does not allow NIO in our code.

So, there are two levels of locking, one is record level, which allows threads to update DIFFERENT records at the same time, the other is file level, which is simply to synchronize all file write operation to ensure the integrity of the file.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with the create method is similar, but not identical to, the other methods in the supplied interface. That is, instead of just acquiring a lock on a given row you need to locate a row and acquire a lock on it. So one solution is:



You will need to be careful with the race condition where two threads both try to lock the same row at the same time. This can be solved by guarding your lock aquisition with a double check locking pattern or similar, and since this code is synch'ed no two threads can be locking any qualifying row at the same time (because no other methods use deleted or new rows).

Raj.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic