Concurrency Notes1
Concurrency Notes1
BlockingQueue Usage
queue.put("1");
bounded.put("Value");
BlockingDeque Implementations
BlockingDeque is an interface.
Thejava.util.concurrent package has the following
implementations of the BlockingDeque interface:
LinkedBlockingDeque
LinkedBlockingDeque
The LinkedBlockingDeque class implements
the BlockingDeque interface.
deque.addFirst("1");
deque.addLast("2");
concurrentMap.put("key", "value");
Implementation class :
ConcurrentSkipListMap
CountDownLatch
A java.util.concurrent.CountDownLatch is a
concurrency construct that allows one or more
threads to wait for a given set of operations to
complete.
A CountDownLatch is initialized with a given count.
This count is decremented by calls to
the countDown()method. Threads waiting for this
count to reach zero can call one of
the await() methods. Calling await()blocks the thread
until the count reaches zero.
Below is a simple example. After the Decrementer has
called countDown() 3 times on the CountDownLatch,
the waiting Waiter is released from the await() call.
Thread.sleep(4000);
public class Waiter implements Runnable{
System.out.println("Waiter Released");
}
}
try {
Thread.sleep(1000);
this.latch.countDown();
Thread.sleep(1000);
this.latch.countDown();
Thread.sleep(1000);
this.latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
CyclicBarrier
The java.util.concurrent.CyclicBarrier class is a
synchronization mechanism that can synchronize
threads progressing through some algorithm. In other
words, it is a barrier that all threads must wait at,
until all threads reach it, before any of the threads
can continue. Here is a diagram illustrating that:
Two threads waiting for each other at CyclicBarriers.
Waiting at a CyclicBarrier
barrier.await();
barrier.await(10, TimeUnit.SECONDS);
ExchangerRunnable exchangerRunnable1 =
new ExchangerRunnable(exchanger, "A");
ExchangerRunnable exchangerRunnable2 =
new ExchangerRunnable(exchanger, "B");
new Thread(exchangerRunnable1).start();
new Thread(exchangerRunnable2).start();
Here is the ExchangerRunnable code:
this.object =
this.exchanger.exchange(this.object);
System.out.println(
Thread.currentThread().getName() +
" exchanged " + previous + " for " +
this.object
);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Semaphore
The java.util.concurrent.Semaphore class is a counting
semaphore. That means that it has two main methods:
acquire()
release()
The counting semaphore is initialized with a given
number of "permits". For each call to acquire() a
permit is taken by the calling thread. For each call
to release() a permit is returned to the semaphore.
Thus, at most N threads can pass the acquire() method
without any release() calls, where N is the number of
permits the semaphore was initialized with. The
permits are just a simple counter. Nothing fancy here.
Semaphore Usage
//critical section
semaphore.acquire();
...
semaphore.release();
Sending Signals Between Threads
Refer :
https://www.mkyong.com/java/java-thread-mutex-
and-semaphore-example/
Q) Difference between Semaphore & Mutex
Java multi threads example to show you how to
use Semaphore and Mutex to limit the number of
threads to access resources.
1.Semaphores – Restrict the number of
threads that can access a resource.
Example, limit max 10 connections to access a
file simultaneously.
2.Mutex – Only one thread to access a
resource at once. Example, when a client is
accessing a file, no one else should have
access the same file at the same time.