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

locking an dynamic Object

 
Stefan Müllerheim
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, i am looking for a solution to lock an object, but ( ) the object is a part of huge Vector, so i can't lock the entire huge Vector. instead i am looking for something like a method. e.g.:



if there is nothing, i will create an seperate array with semaphores or something.

thanks from Germany!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you mean with "locking an object" - you mean that you want to lock it so that only one thread at a time can access it?

You would have to use synchronization for that. See this tutorial: Synchronization
 
Paul Clapham
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Müllerheim wrote:hi, i am looking for a solution to lock an object, but ( ) the object is a part of huge Vector, so i can't lock the entire huge Vector. instead i am looking for something like a method.


That's the wrong "instead". The right "instead" is "Instead, I will extract the object from the Vector and lock it."

Edit: by the way, your terminology indicates there is something you misunderstand. There is no such thing as a "dynamic" object. At least if you assign the usual meaning of "created at run time, not at compile time" to the word "dynamic". All objects are created at run time and I don't know of anything that can be used to characterize one object as "dynamic" and another object as "not dynamic". But you seem to be characterizing objects that way. Can you explain why? That misunderstanding might make your question go away.
 
Stefan Müllerheim
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, for my bad terminology. i will try it better now.

there are lot of objects created to the runtime. therefore it is not possible to build a separate syncronized method for each object. but the object have to lock, due to lot of accessing threads.
my question is: is it possible to lock random objects? e.g. an objectbelonging flag (commonly semaphore) or a method by "lock" and "unlock for random objects?

i have read a lot about syncronization, but every lock is combined with a method or the access of several threads to the same moment, but nothing for a specific period (e.g. read, check, delete a Vector).

isn't been easy to describe my problem. hope it is easier to understand now.


thanks from a dark village in the countryside somewhere in Germany
 
Stefan Müllerheim
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think i have got the solution...sometimes it is so easy, that becomes difficult.

but i need some confirmation for my "awarenesses".

conditions same: reading, checking, writing a Vector securly.

Q1. is it right that only one object may enter a synchronize block depending of the parameter?

Q2. i want overload the methode and synchronize block.



is the sync.block still enterable when an another object with an another(!) parameter try to enter?


A1. code below gave my answer to Q1, dump looks well.

conclusion: i became confused, due to the fact that a method like 'public synchronized void bla( Object obj )' let only one object entering at once, independent of parameter!



thanks for reading!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic