Given shared resources in java, Synchronize resources using CountDownLatch in java. Count down latch is concurrency (multi threading) construct (example).
Semaphore is used to synchronize threads. Semaphores are often used to restrict the number of threads than can access some (physical or logical) resource. We will use binary semaphore to demonstrate the synchronization of shared resources. What is binary Semaphore ? A semaphore initialized to one, and which is used such that it only has… Read More »
Given shared resources in java. Synchronize shares resources using Lock interface. We will use ReentrantLock to protect share resources. A lock is a tool for controlling access to a shared resource by multiple threads. Lock implementations provide more extensive locking operations than can be obtained using synchronized methods and statements. We create five threads, which… Read More »
Given multi-threading application in java. Create Producer & Consumer threads. Producer thread will produces the data. Consumer thread will consumes the data. Synchronize Producer & Consumer thread using wait & notify methods. 1. DataStore class: DataStore class contains LinkedList to store integer values. DataStore class contains two synchronized methods i.e. get & put put method:… Read More »
CopyOnWriteArrayList is thread-safe variant of ArrayList. New copy of underlying array is created when any add, set (or modification) operations performed on CopyOnWriteArrayList. CopyOnWriteArrayList can be visualize as combination of words, COPY + On Write + ArrayList. New copy of underlying array is created, when any write/update/remove operation is performed on CopyOnWriteArrayList. i.e. When any… Read More »
Inter thread communication is very common phenomenon. There are many ways to achieve inter thread communication. In current post, we will use pipes to achieve thread communication. Java has set of classes named PipedOutputStream & PipedInputStream to communicate between threads. We will create couple of threaders namely producer & consumer. Producer thread will produce the… Read More »
Create a task by implementing Runnable interface. We will demonstrate join method and its impact on execution on multiple threads. We will write couple of methods demoThreadJoin: Method will create multiple threads. Output of a method will demonstrates the execution of join method. Threads will execute one after another (i.e. thread-1 to thread-4). demoWithoutThreadJoin: Method… Read More »