Extreme Java When vanilla Java is not enough


Using cache for WordPress

Due to server overload whenever a bot searches my blog, I decided to configure a caching mechanism on my WordPress. Ease as 1-2-3: just install the plugin, configure it (I'm using Amazon CloudFront as CDN), preview and deploy.

Filed under: Uncategorized No Comments

Differences between ArrayList and Vector

Last friday, I posted the differences between HashMap and Hashtable. Studying a little more, I decided to checkout the differences between ArrayList and Vector. Surprise! The same three items, plus one bonus:

  1. Vector is thread-safe, ArrayList isn't;
  2. ArrayList supports null items;
  3. Vector's enumerations are not fail-fast; and
  4. You can pass the capacity increment to Vector's constructor.
Filed under: Java SE No Comments

SwingWorker from JavaSE 6

So, you didn't know about SwingWorker? Neither do I, but it is a very nice addition from JavaSE 6. You can use it as a background thread (instead of Thread class) when your task needs to communicate with Swing.

SwingWorker is abstract and needs you to implement a method "doInBackground" that is the equivalent of "Thread.run". So far, no changes. First difference is that SwingWorker implements "Future", this means it will return the result of processing. Second one is you can override methods like "done" and "process", to work with the result in EDT after the task finishes and partial results, respectively.

SwingWorkers can be cancelled with "cancel(boolean)". This changes a "cancelled" flag that can be checked with "isCancelled". BTW, if the parameter is "true", an old-school "interruption" will also be made on the task.

In "doInBackground", you can also call "progress" (running in worker thread) to report partial results to "process" (in EDT).

SwingWorker has bound properties, specially "progress" (int ranging from 1 to 100, perfect for JProgressBar), and "state" (PENDING, STARTED, DONE), and two type parameters: first for the return type, second for the partial result type (if any).

I need to invest some time in my old projects (like GEBORA) to use this incredible leverage.

BTW, if you need a thread pool, pass SwingWorker to an Executor.

Filed under: Java SE No Comments

Differences between HashMap and Hashtable

Fun facts about HashMap and Hashtable:

  1. Hashtable is thread-safe, HashMap isn't - that's something everybody knows;
  2. HashMap supports null keys and values - not so obvious, but very important;
  3. Enumerators returned from an Hashtable aren't fail-fast. Iterators from both classes are.

That was one of many questions I had to answer in a job interview. I admit I couldn't remember the second and third ones. I think I must study to upgrade my SCJP certification to refresh this kind of detail.

Filed under: Java SE No Comments