Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vectors

 
Giuseppa Cefalu
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
3
C++ Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Giuseppa Cefalu
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
3
C++ Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about LinkedList instead of Vector?
 
Giuseppa Cefalu
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic