StaticPL/JAVA
-
OptionalStaticPL/JAVA 2020. 6. 24. 16:39
1. Overview Optional is a container object used to contain not-null objects. Optional object is used to represent null with absent value. This class has various utility methods to facilitate code to handle values as ‘available’ or ‘not available’ instead of checking null values. It is introduced in Java 8 and is similar to what Optional is in Guava. API Note: Optional is primarily intended for u..
-
Comparable, Comparator, and Collection SortStaticPL/JAVA 2020. 3. 15. 19:24
1. Overview Comparable Comparator Comparable interface is used to sort the objects with natural ordering. Comparator in Java is used to sort the attributes of different objects. Comparable interface compares “this” reference with the object specified. Comparator in Java compares two different class objects provided. Comparable is present in java.lang package. A Comparator is present in the java...
-
Non-blocking, Lock-free operationsStaticPL/JAVA 2020. 2. 27. 17:50
1. Overview In computer science, an algorithm is called non-blocking if failure or suspension of any thread cannot cause failure or suspension of another thread; for some operations, these algorithms provide a useful alternative to traditional blocking implementations. A non-blocking algorithm is lock-free if there is guaranteed system-wide progress, and wait-free if there is also guaranteed per..
-
SemaphoreStaticPL/JAVA 2020. 2. 27. 15:44
1. Overview Semaphore can be used to restrict the number of users to a particular resource or a group of resources, unlike the locks that allow only one user per resource. The semaphore can restrict any given number of users to a resource. 2. Description 2.1 How to use Semaphore semaphore = new Semaphore(NUMBER_OF_PERMITS); semaphore.acquire(5); // NUMBER_OF_PERMITS useResource(); semaphore.rele..
-
ReentrantReadWriteLockStaticPL/JAVA 2020. 2. 27. 11:48
1. Overview 2. ReentrantReadWriteLock 2.1 Why Multiple threads can safely read from a shared resource concurrently as long as they are not modifying its state. 2.2 Usage Synchronized and ReentrantLock do not allow multiple readers to access a shared resource concurrently. Not a big problem in the general case If we keep the critical sections short, the chances of contention over a lock are minim..
-
ReentrantLock, lockInterruptibly, and tryLockStaticPL/JAVA 2020. 2. 27. 11:02
1. Overview Reentrant Locks are provided in Java to provide synchronization with greater flexibility. 2. ReentrantLock 2.1 How to use Lock lockObject = new ReentrantLock(); public int use() throws SomException { lockObject.lock(); try{ someOperations(); return value; } finally { // Ensure releasing lock even though exception occured between locking section lockObject.unlock(); } } 2.2 Why use 2...
-
synchronizedStaticPL/JAVA 2020. 2. 27. 00:43
1. Overview Sometimes, you only want to prevent multiple thread access to part of the code inside a method instead of the entire method. The section of code you want to isolate this way is called a critical section and is also created using the synchronized keyword. 2. Critical section It this way, we can really achieve atomicity for any number of distinct operations without worrying about concu..
-
Atomic VariablesStaticPL/JAVA 2020. 2. 26. 12:34
1. Overview A small toolkit of classes that support lock-free thread-safe programming on single variables. In essence, the classes in this package extend the notion of volatile values, fields, and array elements to those that also provide an atomic conditional update operation of the form 2. Example public class demo { public static void main(String[] args) throws InterruptedException { Inventor..