GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes locking an dynamic Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "locking an dynamic Object" Watch "locking an dynamic Object" New topic
Author

locking an dynamic Object

Stefan Müllerheim
Greenhorn

Joined: Sep 17, 2010
Posts: 12
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!


less is more.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Sep 17, 2010
Posts: 12
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

Joined: Sep 17, 2010
Posts: 12
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!
 
GeeCON Prague 2014
 
subject: locking an dynamic Object