aspose file tools*
The moose likes Threads and Synchronization and the fly likes Vectors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Vectors" Watch "Vectors" New topic
Author

Vectors

Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
Hi,

I have written an application that reads data from a database, displays the data on a JTable that can be modified with edits, insertions and deletions that thereafter update, insert or delete database tables. I am using vector as the underlaying data structure. I have read somewhere that vectos are thread safe, meaning, correct me if I am wrong, that a vector's additions or removal operatons coming from one thread are blocked until another thread completes its additions or removal operations. I am not using threads; this is a single threaded application; but since this application is going to be distributed to several users, and there will be several database connection requests, I have included a serializable isolation statement and implemented transactions so that no dirty reads, phatom reads and no-repetable reads may occur. My question is; since several users are going to be using the application and therefore the application vectors are going to be read and modified by several users at the same time; should the fact that vectors are synchronyzed take care of any problems or is there any thing else I should do to make sure that a vector is not modified in any way until one user's process has completed?

Thank you,

Giuseppa
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 416
    
    2

Vectors give you nothing in this case, they don't work cross process, you have multiple threads just not in the same process and they only communicate via the database.

Presumably its all down to how you have implemented transactions.

Normally you lock the data in the database to prevent concurrent writes such that one user must complete first or allow a subsequent transaction commit to fail based on change having happened (optimistic) and rollback the dirty write.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
Thank you for your response. I do not anserstand very well this part "Vectors give you nothing in this case, they don't work cross process, you have multiple threads just not in the same process and they only communicate via the database." Does this mean that since this is not a multithreaded application, vectors do not take care of synchronization because there is nothing to synchronize and that since the threads are issued by the database, the concurrency control depends in the transactions impementation?

Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 416
    
    2

Vectors would synchronize two threads in the same process only, as your threads are in separate processes / applications they can't communicate with each other and have their own instance of the vector. In a single threaded application the synchronization of vector is not useful (does nothing).
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

The other problem with your original post: whatever you read which said that Vector is "thread safe" was incorrect. It isn't. Sure, all of its methods are synchronized, but that isn't enough to provide full thread safety.

However as Chris says, it's irrelevant in your situation anyway.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
How about LinkedList instead of Vector?
Giuseppa Cefalu
Ranch Hand

Joined: Jul 15, 2011
Posts: 121
Thank you all very much for your responses. As for " How about CopyOnWriterArrayLists instead of Vectors? " What would be the purpose of this in a singlethreaded application?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vectors