Home » Core Java » util » Page 2

util

java.util.concurrent.RejectedExecutionHandler Example

Here we will discuss about the RejectedExecutionHandler Interface in the java.util.concurrent package. This interface is really helpfull when working with the ThreadPoolExecutor. 1. Overview & Usage The interface java.util.concurrent.RejectedExecutionHandler is a handler for tasks that cannot be executed by a ThreadPoolExecutor ( a class that implements ExecutorService, ). This may occur when no more threads or queue slots are available ...

Read More »

java.util.concurrent.ThreadFactory Example

In this example we shall show you how to make use ThreadFactory interface, ThreadFactory is implemented by a user class to override its newThread() method for on-demand creation of a new thread with a specific configuration like thread name, type(user, daemon) and priority, etc.         ThreadFactory vs default ThreadFactory: In a typical Java ExecutorService application where some threads ...

Read More »

java.util.Observer Example

In this example, we shall be discussing about the java.util.Observer interface. The Observer interface is used to notify “Observers” of the changes made to the Obervable. It is mainly used to implement the Observer Pattern in Java. It has been around since Java 1. The Observer interface has only one method, void update(Observable o, Object arg). The Observable o is ...

Read More »

java.util.concurrent.locks.AbstractQueuedSynchronizer Example

AbstractQueuedSynchronizer class has support for exclusive and shared mode of locks and synchronizers. It provides methods for inspection, instrumentation and monitoring methods for condition objects. To implement, the following methods for releasing, acquiring shared, releasing shared, exclusively holding, getting state, setting state and comparing state have to be reimplemented:           tryAcquire(int) tryRelease(int) tryAcquireShared(int) tryReleaseShared(int) isHeldExclusively() getState() ...

Read More »

java.util.PriorityQueue Example

In this example, we shall demonstrate how to use the java.util.PriorityQueue Class. The PriorityQueue Class implements the contract defined through the Queue interface. The PriorityQueue is like other collections as in, it is unbounded and we can specify the starting size. Also, it is not threadsafe in a multi-threaded environment. PriorityQueue Class was introduced since Java 5.       ...

Read More »

java.util.concurrent.ConcurrentNavigableMap Example

In this example we shall show you how to make use ConcurrentNavigableMap Interface, ConcurrentNavigableMap is a NavigableMap which provides navigation methods that returns the closest match for given search targets with a concurrent access support for its submaps. The submaps are the maps returned by various methods like headMap(K toKey), tailMap(K fromKey) and subMap(K fromKey, K toKey).       ConcurrentNavigableMap ...

Read More »

org.apache.commons.io.comparator.DefaultFileComparator Example

Apache Commons IO is a library of utilities to assist with developing IO functionality. org.apache.commons.io.comparator package provides various Comparator implementations for Files. DefaultFileComparator is one of the classes. This class can be used to sort lists or arrays of files (File objects) by using the default file comparison. This article shows usage of this class. The class is from Apache ...

Read More »

java.util.concurrent.atomic.AtomicLongArray Example

In this example we shall show you how to make use of AtomicLongArray class,  AtomicLongArray is a long array in which elements may be updated atomically. In a multithreading concurrent application architecture, we have a few tasks which are parallelized in such a way as to require no coordination between threads. The true problem comes when we need a coordination between ...

Read More »

java.util.concurrent.atomic.AtomicBoolean Example

In this example, we shall be demonstrating the use of java.util.concurrent.atomic.AtomicBoolean Class added in Java 5. The AtomicBoolean class is used to update a boolean value atomically i.e. only one thread can modify the boolean variable at a time. It provides two constructors, a parameterless one with default value as false and a constructor with argument as initial boolean value. ...

Read More »

Java.util.concurrent.CyclicBarrier Example

In one of the previous posts, we talked about CountdownLatch and its use case. Today we are going to talk about a similar yet different synchronisation mechanism called as CyclicBarrier. Java.util.Concurrent.CyclicBarrier is a synchronisation aid that allows a set of threads to wait for each other to reach a common barrier point. This means that all the threads reaching a ...

Read More »