File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes how to make create() allows multi-thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "how to make create() allows multi-thread" Watch "how to make create() allows multi-thread" New topic
Author

how to make create() allows multi-thread

Yan Zhou
Ranch Hand

Joined: Sep 02, 2003
Posts: 137
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

Joined: Sep 02, 2003
Posts: 137
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

Joined: Jun 20, 2003
Posts: 26
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

Joined: Jul 17, 2004
Posts: 356
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


Kai Witte's business website Kai Witte's private homepage
James Turner
Ranch Hand

Joined: May 10, 2004
Posts: 194
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<br />SCJP 1.4 - 92%<br />SCJD - 93%<br />SCWCD 1.4 - 95%<br />SCBCD 1.3 - 100%<br />SCEA - 92%
James Turner
Ranch Hand

Joined: May 10, 2004
Posts: 194
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

Joined: Sep 02, 2003
Posts: 137
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

Joined: Nov 22, 2008
Posts: 18944
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.
 
jQuery in Action, 2nd edition
 
subject: how to make create() allows multi-thread