Home » Core Java » util » concurrent

concurrent

java.util.concurrent.CopyOnWriteArraySet Example

In this post, we are going to discuss about the class java.util.concurrent.CopyOnWriteArraySet and give you and idea of how you can use it on your own code when building robust multi-threaded applications. 1. CopyOnWriteArraySet Class As the name suggests, the CopyOnWriteArraySet class is a replacement for the standard Set class. It uses an internal CopyOnWriteArrayList for all of its operations. ...

Read More »

java.util.concurrent.LinkedBlockingQueue Example

In this example we are going to explain the use of the LinkedBlockingQueue class, and how it is different from the similar ArrayBlockingQueue. The main point of similarity is the concurrent capabilities of both classes, which makes sense as both are part of the java.util.concurrent package. Although they are the most common implementations of the BlockingQueue interface, certain differences need to ...

Read More »

java.util.concurrent.DelayQueue Example

In this example we shall show you how to make use of DelayQueue class. DelayQueue is an unbounded time-based scheduling BlockingQueue of Delayed elements backed by a heap where an element can only be taken when its delay has expired. If multiple elements have expired delays, the element with the longest delay expiration will be taken first.       DelayQueue characteristics: DelayQueue is ...

Read More »

java.util.concurrent.ConcurrentHashMap Example

In this post, we are going to discuss about the class java.util.concurrent.ConcurrentHashMap<K,V> and give you and idea of how you can use it on your own code when building robust multi-threaded applications. 1. ConcurrentHashMap Class The ConcurrentHashMap class provides a concurrent version of the standard HashMap. This is an improvement on the synchronizedMap functionality provided in the Collections class, because ...

Read More »

Java.util.concurrent.SynchronousQueue Example

In this example we will see how to use Java.util.concurrent.SynchronousQueue. SynchronousQueue is a type of Blocking Queue (it implements BlockingQueue) As mentioned in the java docs , it is a blocking queue in which each insert operation must wait for a corresponding remove operation by another thread, and vice versa. This means that the queue can only contain a single ...

Read More »

java.util.concurrent.ArrayBlockingQueue Example

In this post we are going to present the ArrayBlockingQueue class, which implements the BlockingQueue interface. The main reason to use the ArrayBlockingQueue class is that it is thread-safe, in the sense that it can be used concurrently between different threads without any risk. Moreover, it has a specific capacity limit, which can be used in our advantage, e.g. when we ...

Read More »

java.util.concurrent.CopyOnWriteArrayList Example

In this post, we are going to discuss about the class java.util.concurrent.CopyOnWriteArrayList and give you and idea of how you can use it on your own code when building robust multi-threaded applications. 1. Introduction Data structures are a basic element in programming. Almost every program uses one or more types of data structures to store and manage their data. Java ...

Read More »

Java RunnableFuture Example

In this post, we are going to discuss about the class java.util.concurrent.RunnableFuture and give you and idea of how you can use it on your own code when building robust multi-threaded applications. 1. RunnableFuture Class Usually, when you develop a simple, concurrent-programming application in Java, you create some Runnable objects and then create the corresponding Thread objects to execute them. ...

Read More »

java.util.concurrent.RejectedExecutionException – How to solve RejectedExecutionException

In this example we are going to talk about java.util.concurrent.RejectedExecutionException. When using an Executor to run your threads, it might reach a state where it cannot run the task you asked him to. This can happen for a number of reason, as we will demonstrate in this example. It is worth noting that this is a java.lang.RuntimeException       ...

Read More »

Java 8 Parallel Arrays Example

In this article we are going to talk about the new possibilities offered in Java 8 for execute Arrays operations in parallel. We are going to focus on the Arrays class methods. At the end of this article you can download all the examples and snippets (and more). All code has been implemented using Java 8 update 11 and Eclipse ...

Read More »