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 : )