Skip to content


Using one DateFormat instance per thread with ThreadLocal

DateFormat with ThreadLocal

Here is a quick tip how to use DateFormat inside a ThreadLocal field.
Why do we need that? The reason is the DateFormat class is not thread-safe but creating its instances is an expensive operation. So, this is kind of a workaround that creates only one instance of DateFormat per thread.

package com.imsavva.test;

import java.text.SimpleDateFormat;
import java.util.Date;

 * @author Savva Kodeikin
public class ThreadLocalDateFormatTest   {

    private static ThreadLocal<SimpleDateFormat> dateFormat = new ThreadLocal<SimpleDateFormat>() {
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("dd/MM/yyyy");

    public String formatDate(Date date) {
        return dateFormat.get().format(date);

UnhandledExceptionHandler in Java: how to catch uncaught exceptions

UncaughtExceptionHandler in Java

In this short tutorial, I’m going to describe how to handle uncaught exceptions in threads. This can be done by:

  • Using threadInstance.setUncaughtExceptionHandler() ─ for a specific thread
  • Overriding ThreadGroup’s uncaughtException() method ─ for a thread group
  • Using Thread.setDefaultUncaughtExceptionHandler() ─ for all threads

If you’re interested in how to implement this, press “read more”, I’ve prepared an example.

Read More →