Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question on Thread.currentThread()

Status
Not open for further replies.

Kruzer

Programmer
Jun 16, 2000
117
US
I'm experiencing periodically a difference in the number of digits return when calling

Thread thr = Thread.currentThread();
thr.hashcode();

Sometimes the returned value is a 7 or 6 digit number, is there a way to always convert the "thr" value to be an 8 digit number? What do you think of toOctalString(int i)?


I tried something like this to possible eliminate the problem, but I don't think this effective.

int threadValue = 0;
int condition = thr.hashCode() - 10000000;
if (condition < -1) {
// in the event a hascode returns a 7 digit number
threadValue = (thr.hashCode() * 10);
} else {
threadValue = thr.hashCode();
}

Thanks in advance!


Dano
dan_kryzer@hotmail.com
What's your major malfunction
 
Why do you need an 8 digit number ? It makes no sense to me ... the hasCode is there to help in identifying the uniqueness of an object :

/**
* Returns a hash code value for the object. This method is
* supported for the benefit of hashtables such as those provided by
* <code>java.util.Hashtable</code>.
* <p>
* The general contract of <code>hashCode</code> is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the <tt>hashCode</tt> method
* must consistently return the same integer, provided no information
* used in <tt>equals</tt> comparisons on the object is modified.
* This integer need not remain consistent from one execution of an
* application to another execution of the same application.
* <li>If two objects are equal according to the <tt>equals(Object)</tt>
* method, then calling the <code>hashCode</code> method on each of
* the two objects must produce the same integer result.
* <li>It is <em>not</em> required that if two objects are unequal
* according to the {@link java.lang.Object#equals(java.lang.Object)}
* method, then calling the <tt>hashCode</tt> method on each of the
* two objects must produce distinct integer results. However, the
* programmer should be aware that producing distinct integer results
* for unequal objects may improve the performance of hashtables.
* </ul>
* <p>
* As much as is reasonably practical, the hashCode method defined by
* class <tt>Object</tt> does return distinct integers for distinct
* objects. (This is typically implemented by converting the internal
* address of the object into an integer, but this implementation
* technique is not required by the
* Java<font size="-2"><sup>TM</sup></font> programming language.)
*
* @return a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.util.Hashtable
*/
 
The reason is because I have a listening program that expects files to be in an 8 digit format (i.e. 12345678somemethod.txt). I wrote the receive program to expect an 8 digit number for uniqueness. Some how, some way I would like to I guess pad digits to number that are less than 8 digits. With a give or take of 100+ threads being spawned off.

Dano
dan_kryzer@hotmail.com
What's your major malfunction
 
That seems a little inflexible if you ask me, but anyway ...

Code:
		String hc = whatever.hashCode() +"";

		int len = hc.length();
		String tmp = "";
		for (int i = len; i < 8; i++) {
			tmp += "0";
		}

		hc = tmp +hc;
		System.err.println(hc);
 
Actually I suppose incase your hashcode is over 8 digits :

Code:
		String hc = new Object().hashCode() +"";
		//String hc = args[0];
		if (hc.length() > 8) {
			hc = hc.substring(0, 8);
		} else {
			int len = hc.length();
			String tmp = "";
			for (int i = len; i < 8; i++) {
				tmp += "0";
			}

			hc = tmp +hc;

		}

		System.err.println(hc);

But if you do substring it, I wouldn't bet on it being unique ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top