Re: Seeming Non-determinism in Java Programs
Stephen Denne <[email protected]> Mon, 7 Apr 2008 12:56:52 +1200
| Newsgroups | gmane.comp.windows.devel.java.advanced |
|---|---|
| Message-ID | <[email protected]> |
Randall R Schulz wrote:
> So here is the symptom I seek to alleviate: The program's behavior on
> some problems is highly variable. That is, I can invoke the same
> problem repeatedly and get widely varying results. E.g., a given
> problem may be solved in one case in under a thousand nodes,
> in another
> in ten or twenty thousand and in others not at all (with a
> 100,000-node
> limit).
>
> This is perplexing to me. How can multiple independent (but identical)
> invocations of the JVM produce highly varying executions?
>
> My first question is this: What phenomenon / dynamics can cause this
> sort of behavior? While I'm open to the possibility that it is a bug
> (permit me to say a _subtle_ bug), I'm experienced enough to know the
> proper implementation of equals() and hashCode(), e.g.
>
>
> Has anyone else seen this sort of phenomenon? If so, how have
> you dealt
> with it?
I have encountered problems just like this.
I can't recall the cause at the moment, I could probably figure it out based on my solution:
I solved it by changing my usage of HashMap (or indirect use of HashMap within HashSet) to a LinkedHashMap.
That way when iterating over the entries, the returned order is always the same.
The order wasn't important to me, but variations in the order where observed, and variations resulted in different results from various heuristic algorithms.
I think the reasons where due to Object.hashCode()
Important bit from the Javadocs:
(This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
The internal address varied between runs.
Regards,
Stephen Denne.
Disclaimer:
At the Datamail Group we value team commitment, respect, achievement, customer focus, and courage. This email with any attachments is confidential and may be subject to legal privilege. If it is not intended for you please advise by reply immediately, destroy it and do not copy, disclose or use it in any way.
__________________________________________________________________
This email has been scanned by the DMZGlobal Business Quality
Electronic Messaging Suite.
Please see http://www.dmzglobal.com/dmzmessaging.htm for details.
__________________________________________________________________
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com