Home » Core Java » util » Page 4

util

Java.util.TreeSet Example

In this example we will see how and when to use java.util.TreeSet. A TreeSet is a set implementation which provides total ordering on its elements. The elements are ordered using their natural ordering, or by a Comparator typically provided at sorted set creation time. A TreeSet is typically used when we want to keep the elements sorted all times. A ...

Read More »

java.util.concurrent.ForkJoinPool Example

In this example, we shall be demonstrating the use of java.util.concurrent.ForkJoinPool Class. This class was introduced in Java 7. The java.util.concurrent.ForkJoinPool Class implements java.util.concurrent.Executor and java.util.concurrent.ExecutorService interfaces. This class works on divide-and-conquer policy. Each major subtask is divided into a number of sub-tasks and a new thread is spawned for each new sub-task. Recursion is the most popular strategy for ...

Read More »

HashMap changes in Java 8

The way java.util.HashMap entries are indexed and stored has changed in the Java 8 update. Hash elements use balanced trees instead of linked lists under certain circumstances now. All these is what this article is about. The main idea is that when the number of items in a hash is larger than a certain value, the hash will change from ...

Read More »

java.util.Timer Example

In this example we will see how we can use java.util.Timer class to schedule tasks for future execution in a background thread. The tasks may be scheduled for one-time execution, or for repeated execution at regular intervals. This class is thread safe and multiple threads can share a single Timer object without need for external synchronisation. For our example we ...

Read More »

Java AtomicIntegerArray Example

In this example we shall talk about the AtomicIntegerArray class of Java. Java provides the java.util.concurrent.atomic package specification for lock-free and thread-safe programming on single variables. The AtomicIntegerArray class is an int array in which elements may be updated atomically. Its difference with an simple AtomicInteger[] is that it creates one object, whereas when using AtomicInteger[], one object per array ...

Read More »

java.util.concurrent.Semaphore – Semaphore Java Example

In this example, we will show you how to make use of the Semaphore – java.util.concurrent.Semaphore Class in Java. 1. What is a SEMAPHORE? A semaphore in Computer Science is analogous to a Guard at the entrance of a building. However, this guard takes into account the number of people already entered the building. At any given time, there can ...

Read More »

java.util.concurrent.CountDownLatch Example

In this example we will see how and when to use java.util.concurrent.CountDownLatch CountDownLatch is used in synchronisation to allow one or more threads to wait until a set of operations being performed in other threads completes. For Example, it can be used when a Thread has to wait until few dependent threads have started. CountDownLatch is initialised with a given count. This ...

Read More »

Java AtomicInteger Example

This is an example of how to use the AtomicInteger class of Java. The java.util.concurrent.atomic package provides very useful classes that support lock-free and thread-safe programming on single variables. Among them, the AtomicInteger class is a wrapper class for an int value that allows it to be updated atomically. The class provides useful methods, some of which will be shown ...

Read More »

Java ReadWriteLock Example

In this example, we are going to demonstrate the use of ReadWriteLock in Java. Mutually Exclusive Locks, as the ReentrantLock discussed in the previous article, offer far less level of concurrency than non-mutually exclusive locks. If multiple threads are accessing an object for reading data, it does not make sense to use a synchronized block or any other mutually exclusive ...

Read More »

java.util.concurrent.ForkJoinWorkerThread Example

In this post, we are going to discuss about the class java.util.concurrent.ForkJoinWorkerThread and give you and idea of how you can use it on your own code when building robust multi-threaded applications. 1. ForkJoinWorkerThread Class One of the most interesting features of Java 7 is the Fork/Join framework. It’s an implementation of the Executor and ExecutorService interfaces that allow you ...

Read More »