Aug 03

It’s called garbage collection, not resource collection!

Java’s garbage collector has made life so easy for us developers that we sometimes confuse between deleting a reference, i.e. a task the GC does, and releasing a resource – a task the GC doesn’t do. That’s because the GC does something else for us called object finalization, where resources usually clean themselves up.

When I say resources, just think of input/output streams, readers/writers, channels, JDBC, JMS.. the list could go on and on. I almost expect to see code such as: Continue reading »

  • Share/Bookmark
Sep 14

A class I find missing in the new java.util.concurrent package is PriorityBlockingDeque. Just like PriorityBlockingQueue, this class should be sorting its elements either by their natural order or by a supplied Comparator.

I fail to understand the reason for having this class obviously missing from the package, and because I need it very much, I decided to create a blocking wrapper around NavigableSet using locks and conditions. This uses NavigableSet’s already existing methods of pollFirst, pollLast, first and last to fulfil the Deque interface.

Update: After some comments appeared I’ve realised that by using NavigableSet I do not allow for duplicate values on the Deque. Therefore, I’ve changed the implementation to use LinkedList internally, using Collections.sort calls to keep the list sorted. Unfortunately, this brings the basic add operation to O(n log(n)), instead of the O(log(n)) it used to be.

The code is fully available here, as part of the collections project, and some unit tests are available here.

I will later post how I used it, but let me know if you used it and if it was of any help (or filled with bugs..)

  • Share/Bookmark
Jul 02

Ever wanted to be able to know your process’ PID through pure-Java, cross-platform methods? Or worse, other JVMs?

At my work we came to a point where we needed to monitor and register a JVM’s PID (Linux/Windows/Whatever) for some future manipulation purposes. However, Java in itself did not contain the concept of “process ID” – Even the Process class (which is horrible, for different reasons which might be discussed on a different post) doesn’t have any way of getting that information.

However, JavaVM Process Status Tool (JPS) seems to be written entirely in Java and is actually able to retrieve this information! Jps is a Java class located on Linux and Windows in the jdk/lib/tools.jar file, or in the CurrentJDK/Classes/classes.jar if you’re using a Mac. Also, it’s only bundled with JDK 5 and up, which I assume that by now, all of the readers have installed.
Continue reading »

  • Share/Bookmark

Chaotic Java is Digg proof thanks to caching by WP Super Cache