I was reading this : http://tutorials.jenkov.com/java-concurrency/read-write-locks.html
Inside this tutorial ,for writing a re entrant read lock,following code has been used
public class ReadWriteLock{
private Map<Thread, Integer> readingThreads =
new HashMap<Thread, Integer>();
private int writers = 0;
private int writeRequests = 0;
public synchronized void lockRead() throws InterruptedException{
Thread callingThread = Thread.currentThread();
while(! canGrantReadAccess(callingThread)){
wait();
}
readingThreads.put(callingThread,
(getAccessCount(callingThread) + 1));
}
public synchronized void unlockRead(){
Thread callingThread = Thread.currentThread();
int accessCount = getAccessCount(callingThread);
if(accessCount == 1){ readingThreads.remove(callingThread); }
else { readingThreads.put(callingThread, (accessCount -1)); }
notifyAll();
}
private boolean canGrantReadAccess(Thread callingThread){
if(writers > 0) return false;
if(isReader(callingThread) return true;
if(writeRequests > 0) return false;
return true;
}
private int getReadAccessCount(Thread callingThread){
Integer accessCount = readingThreads.get(callingThread);
if(accessCount == null) return 0;
return accessCount.intValue();
}
private boolean isReader(Thread callingThread){
return readingThreads.get(callingThread) != null;
}
}
So I have question,can i use hashmap like this
private Map<Thread, Integer> readingThreads =
new HashMap<Thread, Integer>();
I checked Thread class code to check if it overrides equals and hashcode and find that it doesnt. So will it be defaut equals and hashcode. So all will point to same bucket ??
Can anyone please help me understanding how it will be mapped to buckets. Also,what is needed from programmer side to use thread object as key to hashmap?