473,471 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

152 New Member
This is the hashCode() from the openjdk String class:

Expand|Select|Wrap|Line Numbers
  1. public int hashCode() {
  2.         int h = hash;
  3.         if (h == 0) {
  4.             int off = offset;
  5.             char val[] = value;
  6.             int len = count;
  7.  
  8.             for (int i = 0; i < len; i++) {
  9.                 h = 31*h + val[off++];
  10.             }
  11.             hash = h;
  12.         }
  13.         return h;
  14. }
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??
Oct 15 '11 #1
0 1179

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: pentium77 | last post by:
Hi, Just wondering what's the algo used to compute hashcode in Java ? Does anybody here know ? Thanks, -MK.
4
by: Nick | last post by:
Hi all, I am using GetHashCode on unique strings to get a unique integer for a string that I can then place into a database (use int rather than the string to make indexing faster). The problem...
9
by: John | last post by:
If a value type is immutable, I guess it's threadsafe to read it? But not threadsafe to assign a new value to it (can any value type be truely immutable? Isn't assigning a totally new value to it,...
3
by: Diffident | last post by:
Hello All, Following is a static method. Can you please tell me if this is threadsafe...why? If it is not threadsafe...why? Thank you. public static string...
8
by: MuZZy | last post by:
Hi, Why for god sake they change implementation of String.GetHashCode() from ..NET 1 to .NET 2? We were storing some user passwords in hashcode, now we can't upgrade those clients with .NET 2...
9
by: archana | last post by:
Hi all, I have one question regarding hashing in .net I have two string containing same data. When i see hashcode by calling gethascode, i am getting same value. I want to know how...
1
by: Alfredo Mesen | last post by:
I'm giving maintenance to someone elses sloppy code, on a servlet´s doPost it compares an input to determine the action that must be done, like if (action.equals("delete")) { ... } so was...
6
by: Mike9900 | last post by:
Hello, I am wondering why GetHashCode() for the string is using two differemt algorithms in .NET Framework 1.1 and 2. This is creating a big problem, because we relied on this hashcode as...
1
by: praveenkumarvpk | last post by:
public static void main(String args) { String elements = { "A", "B", "C", "D", "E" }; Integer l = {1,2,68}; Integer y=1; int d=0; for(int i=0;i<l.length;i++) ...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.