Home » Core Java » util » Page 3

util

java.util.concurrent.RecursiveTask Example

In this example we shall show you how to make use of Java RecursiveTask class, RecursiveTask provides a convenient way to consolidate results from subtasks. Java 7 introduced another implementation of ExecutorService: the ForkJoinPool class. ForkJoinPool is designed for handling tasks efficiently that can be repeatedly broken down into subtasks, using the RecursiveAction class (when the task produces no result) or ...

Read More »

Java AtomicMarkableReference Example

In this example we shall make use of AtomicMarkableReference class of Java. It is another class under the java.util.concurrent.atomic package, which provides lock-free and thread-safe programming solutions. In a recent example, we have seen how the AtomicReference class can be utilized to provide reference objects that may be atomically updated. Atomic actions are actions performed in an object only by ...

Read More »

Java.util.EnumMap Example

In this example we will see how and when to use Java.util.EnumMap. EnumMap is used when we need to have keys of the map as enums. EnumMaps are represented internally as arrays. This representation is extremely compact and efficient. Enummaps are maintained in the natural order of their keys (the order in which the enum constants are declared). Lets see ...

Read More »

java.util.concurrent.Exchanger Example

In this example, we shall be demonstrating how we can use the java.util.concurrent.Exchanger Class in Java. The Exchanger Class provides a sort of rendezvous point for two threads, where the threads can exchange their respective Objects with the other thread. Whenever a thread arrives at the exchange point, it must wait for the other thread to arrive. When the other ...

Read More »

java.util.concurrent.ThreadLocalRandom Example

In this example we shall show you how to make use of ThreadLocalRandom class, ThreadLocalRandom is a random number generator like its parent Random class. Like the global Random generator class, a ThreadLocalRandom is initialized with an internally generated seed that may not otherwise be modified.         1. ThreadLocalRandom Advantages ThreadLocalRandom provide more good performance and less overhead in ...

Read More »

java.util.concurrent.ScheduledThreadPoolExecutor Example

In this example, we shall be demonstrating how we can use the java.util.concurrent.ScheduledThreadPoolExecutor Class to schedule some periodic Tasks at fixed rate or fixed delay. 1. Timer Timer task is used also used to schedule activities. However, the Timer object can run only Task at a time. As a result of which, if the previous task is taking a more ...

Read More »

Java Treemap – java.util.TreeMap Example

1. Introduction In this example we will see how and when to use the Java Treemap class java.util.TreeMap. A TreeMap is a Red-Black tree based NavigableMap implementation which has log(n) time cost for the basic operations: add, remove, and contains. A TreeMap guarantees that the elements inserted remains sorted on the order of keys. The elements are ordered using the ...

Read More »

java.util.concurrent.FutureTask Example

In this example we shall show you how to make FutureTask, FutureTask is an implementation of Future which offers a very elegant way to implement parallel execution of tasks in Java where a big task can be split into small chunks and if each of those chunks can be executed in parallel, it can result in better response times and throughput. We can ...

Read More »

java.util.concurrent.Phaser Example

In this example, we shall be demonstrating the java.util.concurrent.Phaser Class. The Phaser Class was introduced in Java 7. Phaser is like a collection of advantages of CountDownLatch and CyclicBarrier Classes.             CountDownLatch vs CyclicBarrier vs Phaser The CountDownLatch is : Created with a fixed number of threads Cannot be reset Allows threads to wait(CountDownLatch#await()) or ...

Read More »

Java AtomicReference Example

This is an example of the AtomicReference class of Java. It is another class under the java.util.concurrent.atomic package, which provides an object reference that may be updated atomically. The AtomicReference class provides reference objects that may be read and written atomically, so when multiple threads try to reach them at the same time, only one will be able to do ...

Read More »