2

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?

1 Answer 1

4

So all will point to same bucket ?

No. In java, every class(including Thread) implictly extends from Object class. The equals and hashCode method extend from Object class are enough to make the Thread objects distribute in different buckets.

See the doc of Object.equals:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

and Object.hashCode:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.