Enum Hash Codes Are Non-Deterministic

An annoying discovery: Java Enum objects have non-deterministic hash codes. Specifically, a hash code is the Enum's identity (or memory address).

public final int hashCode()
{
    return super.hashCode();
}

So, should you ever make the same mistake as me, keying a HashMap with an Enum, strongly consider using EnumMap, instead.

No ConcurrentHashSet? No Problem

The JDK provides several concurrent Set classes, each with their own application, but it does not provide a ConcurrentHashSet. Using the Collections utility class, however, such a structure may be created.

If you have ever looked at the source of java.util.HashSet, you will have noticed that it is ...

Using @cache_page With Django's Feeds

When returning a django.contrib.syndication.Feed from a view function, the @cache_page decorator does not seem to work as expected. Briefly inspecting the source of Feed explains why: Feed is not an HttpResponse. Truly, this isn't a bug: it's a documentation (if not comprehension) issue. Because the ...

The Concurrency Of ConcurrentHashMap

Java's ConcurrentHashMap is an excellent point of study when learning about Java's Memory Model. It employs numerous techniques to provide lock-free reads while still guaranteeing safe publication of objects. This article represents my attempt to take the magic out of the ConcurrentHashMap implementation.

This article covers the java ...

Escape Analysis & Stack Allocated Objects

In 1999, a paper, Escape Analysis For Java was to appear in the ACM SIGPLAN Conference on Object-Oriented Programming Systems, Languages, and Applications. This paper outlined an algorithm to detect whether an object, O, escaped from the currently executed method, M, or current thread, T. This paper laid the groundwork ...

Impact Of Local Variable Declaration Location

Does the location of a local variable's declaration result in performance changes of a method? For example, will a String variable, s, declared outside of a loop exhibit "better" performance than when declared inside a loop? In Java, looking at a class' bytecode explains, irrefutably, why the answer is ...

Safe Publication Of Transient Members

A very common (and quite dangerous) tripping point I find in concurrent Java code is that of unsafe member publication of otherwise safely-published objects. Of course, concurrent code is difficult to get right and I occasionally find myself identifying the same mistake in my own code---but usually only after a ...

Redirecting "derby.log" To An SLF4J Logger

More and more, I find myself using Apache Derby as an embedded database for personal projects. One inconvenience, however, is the persistent derby.log file than inevitably gets created at startup. It can, of course, be disabled entirely, but I'd like Derby errors to be put into my application ...

Tracing Servlet Requests

Tracing through Servlet-based web application log files isn't always as convenient as one may hope. Sure, when using Tomcat, we may rely on the thread names (such as http-8080-2), but sometimes they seem to bleed into each other resulting in a difficult structure to grep. Compounding this, an observer ...

Suppressing An Appender For A Log4J Logger

Occasionally, a third-party library will balk the log-or-throw exception handling convention. Most recently, I came across this issue with Hibernate's Work API: an org.hibernate.exception.ConstraintViolationException was being logged and thrown when a unique key constraint was violated. To be exact, Hibernate's JDBCExceptionHelper logged exceptions through JDBCExceptionReporter ...


1 2 3