Home » Core Java » util » Page 5

util

java.util.concurrent.locks.Condition Example

In this tutorial we will discuss about the Condition interface in Java. A Condition object, also known as condition variable, provides a thread with the ability to suspend its execution, until the condition is true. A Condition object is necessarily bound to a Lock and can be obtained using the newCondition() method. Furthermore, a Condition enables the effect of having ...

Read More »

Java ReentrantLock Example

In this example, we shall be demonstrating how to use ReentrantLock in Java. Rigidness of Intrinsic Locking : The traditional way of providing synchronization in multi-threaded environment was to use the synchronized keyword. However, the synchronized keyword is considered rather rigid under certain circumstances.For example, if a thread is already executing inside a synchronized block and another thread tries to enter the ...

Read More »

java.util.LinkedHashSet Example

In this post, we are going to discuss about the class java.util.LinkedHashSet and give you an idea of how you can use it on your own code when you want to manage collections of objects. Applications often must manage collections of objects. Although you can use arrays for this purpose, they are not always a good choice. For example, arrays ...

Read More »

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.Date to java.sql.Date

In this example, we shall show you how to convert a java.util.Date object to a java.sql.Date object. This conversion is usually necessary when a Date object needs to be written in a database. java.util.Date represents a specific instant in time, with millisecond precision. It represents both date and time. java.sql.Date is a wrapper around millisecond value and is used by ...

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 »