Is this hashCode() thread-safe from String??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tassos Souris
    New Member
    • Aug 2008
    • 152

    Is this hashCode() thread-safe from String??

    This is the hashCode() from the openjdk String class:

    Code:
    public int hashCode() {
            int h = hash;
            if (h == 0) {
                int off = offset;
                char val[] = value;
                int len = count;
    
                for (int i = 0; i < len; i++) {
                    h = 31*h + val[off++];
                }
                hash = h;
            }
            return h;
    }
    Is this implementation thread-safe?? With the exception that many threads could compute the hash (and even all of them) but none of them could get a erroneous value from the h = hash and hash = h assinghments right??

    Also, does the hashCode() must return only positive integers??

    Finally, what if there is an overflow in the int arithmetic there? How to handle this case in our hashCode() implementations ??
Working...