Posts

Showing posts with the label Caching

CountDownLatch vs CyclicBarrier vs Semaphore - Java

Image
CountDownLatch CyclicBarrier Semaphore CountDownLatch and CyclicBarrier on the first look seems to do the same task and I read some blogs where they have tried to explain the difference of the two. Most common differences many were saying were; CountDownLatch can not be reused after meeting the final count. CountDownLatch can not be used to wait for Parallel Threads to finish. CyclicBarrier can be reset thus reused CyclicBarrier can be used to wait for Parallel Threads to finish. From these explanation the picture I got was this; If a Main thread creates 5 different thread. CountDownLatch can be used by the Main Thread to wait on the Child Threads. Where as CyclicBarrier can be used to enable waiting on Threads until each other finish. Thus CountDownLatch is a top down waiting where as CyclicBarrier is across waiting. But this wasn't convincing enough for me because no blog clearly explained the practical usage of the two classes. Furthermore there weren't not ...

LRU Implementation - Java

import java.util.LinkedHashMap ; import java.util.Map ; /** * Created by skupunarapu on 10/19/2015. */ public class LRUImpl extends LinkedHashMap<Integer , String> { private static final long serialVersionUID = 1L ;   private int capacity ; public LRUImpl( int capacity , float loadFactor){ super (capacity , loadFactor , true ) ;   this . capacity = capacity ; } @Override protected boolean removeEldestEntry (Map.Entry<Integer , String> eldest) { return size() > this . capacity ; } public static void main (String arg[]){ LRUImpl lruCache = new LRUImpl( 4 , 0.75f ) ; lruCache.put( 1 , "Object1" ) ; lruCache.put( 2 , "Object2" ) ; lruCache.put( 3 , "Object3" ) ; lruCache.get( 1 ) ; lruCache.put( 4 , "Object4" ) ; System. out .println(lruCache) ; lruCache.put( 5 , "Object5" ) ; ...