using a private variable for the lock of synchronized block
-
hi there i am trying to implement a simple chatserver and when searching for examples i saw a synchronized statement which uses a variable as a lock of the block.. the code is this;
private List myList = new ArrayList();
private Map();public void put( String s, Bar b ) {
synchronized( myMap ) {
myMap.put( s,b );
// then some thing that may take a while like a database access or RPC or notifying listeners
}
}public void hasKey( String s, ) {
synchronized( myMap ) {
myMap.hasKey( s );
}
}public void add( Foo f ) {
synchronized( myList ) {
myList.add( f );
// then some thing that may take a while like a database access or RPC or notifying listeners
}
}public Thing getMedianFoo() {
Foo med = null;
synchronized( myList ) {
Collections.sort(myList);
med = myList.get(myList.size()/2);
}
return med;
}so i cant understand why and how can be a list be a lock. i always use "this" for accessing a synchronized statement. i appreciated if you can help me and thanks anyway : )
-
hi there i am trying to implement a simple chatserver and when searching for examples i saw a synchronized statement which uses a variable as a lock of the block.. the code is this;
private List myList = new ArrayList();
private Map();public void put( String s, Bar b ) {
synchronized( myMap ) {
myMap.put( s,b );
// then some thing that may take a while like a database access or RPC or notifying listeners
}
}public void hasKey( String s, ) {
synchronized( myMap ) {
myMap.hasKey( s );
}
}public void add( Foo f ) {
synchronized( myList ) {
myList.add( f );
// then some thing that may take a while like a database access or RPC or notifying listeners
}
}public Thing getMedianFoo() {
Foo med = null;
synchronized( myList ) {
Collections.sort(myList);
med = myList.get(myList.size()/2);
}
return med;
}so i cant understand why and how can be a list be a lock. i always use "this" for accessing a synchronized statement. i appreciated if you can help me and thanks anyway : )
You can lock any object. In principal, the lock object can only be accessed by the thread that took the lock. If you are accessing an object that is not thread safe, that can be called from multiple threads, then you should always lock that object.
Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
-
hi there i am trying to implement a simple chatserver and when searching for examples i saw a synchronized statement which uses a variable as a lock of the block.. the code is this;
private List myList = new ArrayList();
private Map();public void put( String s, Bar b ) {
synchronized( myMap ) {
myMap.put( s,b );
// then some thing that may take a while like a database access or RPC or notifying listeners
}
}public void hasKey( String s, ) {
synchronized( myMap ) {
myMap.hasKey( s );
}
}public void add( Foo f ) {
synchronized( myList ) {
myList.add( f );
// then some thing that may take a while like a database access or RPC or notifying listeners
}
}public Thing getMedianFoo() {
Foo med = null;
synchronized( myList ) {
Collections.sort(myList);
med = myList.get(myList.size()/2);
}
return med;
}so i cant understand why and how can be a list be a lock. i always use "this" for accessing a synchronized statement. i appreciated if you can help me and thanks anyway : )
the list used as a lock is an instance of class ArrayList. According to specification ArrayList implementation, an Arraylist is not synchroized, ie if multiple threads start accessing and or modifying the arraylist the state(data) of arraylist may get corrupted. So the instance "list" is used as lock so that block of code is synchronized and only one thread can access the list at any given point of time. do rate the answer or correct me if i am wrong. thanx Shreyas Rane