Category Archives: java

Producer, Consumer with ReentrantLock, Condition

public class PC { public static void main(String[] args) { ReentrantLock lock = new ReentrantLock(); Condition added = lock.newCondition(); Condition removed = lock.newCondition(); int[] resource = new int[1]; Consumer c1 = new Consumer(lock, added, removed, resource); Producer p1 = new Producer(lock, added, removed, resource); Producer p2 = new Producer(lock, added, removed, resource); new Thread(c1).start(); new… Read More »


Each segment has multiple entries. Each entry, it starts with a LinkedList. When it has too many, it changes to Black-Red-Tree. When write, if two writes are for different segment, then it allows. When multiple writes to same segment, it uses CAS(compare-and-set) to write and handle conflict. Each operation on segment has a counter. The… Read More »

ReentrantLock, ReentrantReadWriteLock

RentrantLock is almost the same as synchronized keyword. Only difference is that synchrnozied keyword needs a block. It maintains a queue for the threads. RentrantLock lock = new ReentrantLock(); lock.lock(); try { do something.. } finally { lock.release(); } ReentrantReadWriteLock. It also maintains a queue for threads. When it has queue: [readThread1, readThread2, writeThread3, readThread4]… Read More »

Background run, nohup, &

nohup, means “no hangup”. Means even logout, let it run. &, run in background. For instance: nohup java -jar abc.jar &

wait(), notify(), notifyAll(), sleep()

wait(), notify(). obj.wait() will release the object’s lock, until some other thread calls obj.notify(). When calling obj.wait(), current thread should own obj. Normally, it is called like: synchronized(obj) { // acquire obj obj.wait(); } IllegalMonitorStateException will be thrown if current thread doesn’t own obj. obj.notify() wakes up single thread which waits for obj lock. obj.notifyAll()… Read More »

Java Coding Style

Guava: Preconditions.checkNotNull Guava: Preconditions.checkArgument JDK: Objects.requireNotNon NullPointerException or IllegalArgumentException 1. Many APIs in the JDK pro-actively throws NullPointerException. This suggests that throwing NPE is a Java convention. 2. Effective Java Item 60: “Arguably, all erroneous method invocations boil down to an illegal argument or illegal state, but other exceptions are standardly used for certain kinds… Read More »

default keyword in Java

default can be used in switch statement. default can be used in annotation, specifying the default value. For example: @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @interface Todo { String author() default “Yash”; } @Todo(author = “Yashwant”) public void incompleteMethod1() { //Some business logic is written //But it’s not complete yet }


public static void converterTest() { BiMap<Integer, String> biMap = HashBiMap.create(); biMap.put(1, “a”); biMap.put(2, “b”); biMap.put(3, “c”); Converter<Integer, String> converter = Maps.asConverter(biMap); System.out.println(converter.convert(1)); System.out.println(converter.reverse().convert(“b”)); }