Threading

Do Not Assume SimpleDateFormat Is Thread-safe

The code below is improper because it shares a static instance of a SimpledateFormat with any possible number of threads. Public class Constants { Public static final SimpleDateFprmat date = new SimpleDateFormat(“dd.MM.yyyy”);} SimpleDateFormat is not thread-safe; if multiple threads concurrently use this object, the result is undefined and the output

Obtaining a Thread Dump of Your Java App in Linux

Thread dump is a mechanism to view the various threads active in your application and also to know the current methods that they are active on. The result may not be the same every time you take a thread dump as the state can be different at different intervals. In

Keeping Tabs on all Threads in Your Environment

Oftentimes, you might want to understand and control all the threads in your runtime. Java provides an effective and easy mechanism to achieve this using Thread.activeCount() and Thread.enumerate() methods. public class KnowAllThreads { public static void main(String args[]) { Thread myThread = Thread.currentThread(); myThread.setName(“My Thread”); myThread.setPriority(1); System.out.println(“Current thread: ” +

Finding the Count of All Active Threads

Most developers work with threads at one point or another. It can be important to know which threads are active to accomplish a given task. The following API activeCount on the Thread class returns the count of the active threads in the current run time. public class ActiveThreadsCount{ public static

Using ThreadMXBean to Understand Threads

The java.lang.management.ThreadMXBean is an interface that helps us to understand a lot about Threads in our code. There are numerous methods that participate to provide excellent visibility of the Threads. Something that caught me by surprise was the method findDeadlockedThreads() that returns a list of all Threads that are in dead-lock at

Reading Updated/Latest Value in Multithreading Environment

In a multithreaded environment, there are always doubts about the latest value that a variable is holding, since there can be numerous thread instances updating, and also holding localized copies of the same variable, to consider. Try to use the volatile keyword and the magic is visible. Using the volatile

Making a Java List Thread Safe

The java.util.list is not thread safe by default. There can be situations where you would want to use the list in a multithreaded environment for processing. An effective mechanism is to use Collections.synchronizedList(ordinaryList) where ordinaryList is a normal list. The return value of this invocation is a thread safe list