Archives for posts with tag: java

What began as a small peek at JDK source code quickly showed me a whole new side of Java.

java.util.concurrency.atomic.LongAdder is a new class that will be introduced with Java 8. It provides an atomic way to add up a large number of values. According to the documentation, LongAdder is faster than AtomicLong. I was curious how that could be and had a look at the implementation.


I found that yes, LongAdder is faster (see graph on the right). And I also learned a bit about how atomics are implemented in Java. It turned out to be more complex and more interesting than I expected.

Read the rest of this entry »


This is part 3 of a series on patterns for writing safe constructors (and destructors) in C# and Java. Start reading at part 1: Exceptions in Constructors.

As seen in part 2, using the Open / Close pattern decouples object creation and resource allocation safely. However, for this solution to work we need to rely on the user of the object to stick to the correct sequence of actions, or we need to explicitly enforce the correct sequence with invariants.

Static factory methods reduce the potential problems we introduced in part 2. This is achieved by combining object creation and calling the Open method. Many of the problems listed at the end of part 2 simply disappear.

Read the rest of this entry »

This is part 2 of a series on patterns for writing safe constructors (and destructors) in C# and Java.

As seen in Part 1, we can have a problem if a constructor throws an exception after allocating a resource. There is no trivial way of releasing the resource after the constructor bails out. Part 2 discusses the most common pattern to avoid this situation: moving the allocation of resources into an Open() method.

Read the rest of this entry »

What happens if a constructor throws an exception? Is a new object still created? Do we even need to care?

Yes, a new object is created. It won’t be initialized properly and we usually won’t have a reference to it. And yes, we do need to care. That half initialized object may block important resources.

The examples in this series are in C#, but everything can be applied to Java and other similar object oriented languages.

Read the rest of this entry »

I hacked together an Eclipse @Autowired plugin for a 24 hour innovation day challenge at work. I had not written an Eclipse plugin before and it turned out to be a more complex task than I imagined.

What I like about IDEs is that I can navigate quickly between references, declarations, definitions, superclasses, and subclasses. Pretty much all code relationships that are known at compile time can be navigated. Dependency injection, which I use all the time when I program something in Spring, establishes relationships at runtime and IDEs (or at least Eclipse, which is what I mainly use) don’t know anything about them. There seem to be Eclipse plugins that visualise these relationships but what I want is to right-click on a field and ask Eclipse to take me to the definitions of the bean that’s autowired into it (yes, we mainly use the @Autowired annotation at work). What I don’t want is to open a separate view or even a graph showing bean relationships. I want to see them in the code just like normal code relationships.

Read the rest of this entry »