Skip to main content

Posts

Reentrant Locks

Synchronized uses intrinsic locks or monitors. Every object in Java has an intrinsic lock associated with it. Whenever a thread tries to access a synchronized block or method, it acquires the intrinsic lock or the monitor on that object. In the case of static methods, the thread acquires the lock over the class object. ReentrantLock is an explicit locking mechanism.  A reentrant mutual exclusion Lock with the same basic behavior and semantics as the implicit monitor lock accessed using synchronized methods and statements, but with following extended capabilities. Polled and Timed Lock Acquisition Lock.tryLock() is polled lock acquisition mechanism and tryLock(long timeout, TimeUnit unit) is timed lock acquisition mechanism. tryLock() will return immediately. If no other thread locked, then it will return true else returns false. The tryLock(long timeout, TimeUnit unit) will wait for the timeout period to acquire the lock if the lock is not available until timeout pe
Recent posts

Binary search (recursive and non-recursive)

Recursive solution: public static int binarySearch( int [] array , int startIndex , int endIndex , int value ){ int midIndex = ( startIndex + endIndex )/2; if ( array [ midIndex ] == value ){ return value ; } if ( startIndex > endIndex ){ return -1; } if ( value < array [ midIndex ]){ int sI = startIndex ; int eI = midIndex - 1; return binarySearch( array , sI , eI , value ); } else { int sI = midIndex + 1; int eI = endIndex ; return binarySearch( array , sI , eI , value ); } } Non-Recursive solution: public static int binarySearchNoRecursion( int [] array ,  int value ){ int startIndex = 0; int endIndex = array . length - 1; while ( startIndex <= endIndex ){ int midIndex = ( startIndex + endIndex )/2; if ( array [ midIndex ] == value ){ return value ; } if ( value < ar

Count number of coins needed

Write a method to return the minimum number of coins needed to match a given value. Assume coin denominations are 25, 10, 5, 1. Example: If the value is 101, then we require four 25 cents coins and one 1 cent coin, so total is 5 coins. public static int coinCount( int value ){ int [] denominations = {25, 10, 5, 1}; int coinCount = 0; for ( int i = 0; i < denominations . length ; i ++){ if ( value % denominations [ i ] == 0){ coinCount = coinCount + ( value / denominations [ i ]); break ; } if ( value % denominations [ i ] > 0){ int r = value / denominations [ i ]; coinCount = coinCount + r ; value = value % denominations [ i ]; } } return coinCount ; }

Merge two sorted Lists or Arrays

Code to merge two array lists: public static ArrayList<Integer> mergeLists(ArrayList<Integer> l1 , ArrayList<Integer> l2 ){ ArrayList<Integer> l3 = new ArrayList<Integer>(); int i = 0; int j = 0; while ( i < ( l1 .size()) && j < ( l2 .size())){ if ( l1 .get( i ) < l2 .get( j )){ l3 .add( l1 .get( i )); i ++; } else { l3 .add( l2 .get( j )); j ++; } } if ( i < l1 .size()){ while ( i < l1 .size()){ l3 .add( l1 .get( i )); i ++; } } else if ( j < l2 .size()){ while ( j < l2 .size()){ l3 .add( l2 .get( j )); j ++; } } return l3 ; } Code to merge two arrays: public static int[] mergeArrays(int[] a, int[] b){ if((a == null || a.length == 0) && (b == null || b.length == 0)) return new int[0]; if((a == null || a.length == 0)) return b; if(b == n

Find second largest element in an array

Find second largest element in an unsorted array.               We can reach the solution in three ways.  Solution 1 Sort the array and return last but one element. We could use already existing sorting methods in JDK Arrays class and sort the array. public static int findSecondLargestV1( int [] array ){ if ( array . length == 0 || array . length < 2){ return -1; } Arrays.sort( array ); return array [ array . length - 2]; } Complexity: Since this solution depends on sorting, the complexity is equal to complexity of sorting algorithm, ie., O(n log n) Solution 2 public static int findSecondLargestV2( int [] array ){ if ( array . length == 0 || array . length < 2){ return -1; } int max = -1; for ( int i = 0; i < array . length ; i ++){ if ( array [ i ] > max ){ max = array [ i ]; } } int secondMax = -1; for (

Java Script

Java Script Tutorial: http://www.w3schools.com/js/default.asp How Java Script Works: http://misko.hevery.com/2010/07/14/how-javascript-works/ Java Scripting Programmer's Guide: http://download.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html#helloworld Rhino Documentation: http://www.mozilla.org/rhino/doc.html

Useful Java Related Resources

Java Tech Videos: http://www.techonvideo.com/category/programming/java/ Java Annotations: http://www.developer.com/java/other/article.php/3556176/An-Introduction-to-Java-Annotations.htm http://download.oracle.com/javase/tutorial/java/javaOO/annotations.html