Posts

Java Program for a Deadlock

public class DeadLock{ public static void main(String[] args) throws InterruptedException { final Object obj = new Object(); final Object obj1 = new Object(); Thread t = new Thread() { public void run() { while (true) { synchronized (obj) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (obj1) { } } } } }; t.start(); while (true) { synchronized (obj1) { Thread.sleep(1000); synchronized (obj) { } } } } } Login with Facebook

Java Program for Langford Sequence of a given number

public class Langford {     private static boolean print(int array[], int k, int n) {         if (k == n + 1) {             return true;         }         for (int i = 0; i < array.length; ++i) {             if (array[i] == 0 && i + k + 1 < array.length && array[i + k + 1] == 0) {                 array[i] = array[i + k + 1] = k;                 if (print(array, k + 1, n)) {                     return true;                 }                 array[i] = array[i + k + 1] = 0;             }         }         return false;     }    ...

Multi Thread Synchronization by locks in java

Following java code prints natural numbers in sequence by multiple threads. Number of threads can be changed by the field "numOfThreads". If numOfThreads is 3, output will be like: Thread 1 -1, Thread 2- 2, Thread 3- 3, Thread 1- 4, Thread 2- 5 and so on till a max value defined by field "maxValue". package multi.thread.abc; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class MultiThreadSync { private static final int numOfThreads = 4; private static final int maxValue = 20; private final CyclicBarrier barrier; private final Counter counter; private final Thread[] threads; public MultiThreadSync () { counter = new Counter(numOfThreads, maxValue); barrier = new CyclicBarrier(numOfThreads + 1); threads = new Thread[numOfThreads]; for (int ...

Odd Even and Alphabet program in Java by 3 or more threads

Following program can print the below series: 1,2,a,3,4,b,5,6,d..... (By 3 threads) 1,2,3,a,4,5,6,b,7,8,9,d... (By 4 threads) 1,2,3,4,5,6,a,7,8,9,10,11,12,b.... (By 7 threads) Number of threads and max value can be set by their respective fields. package multi.thread.abc; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /**  *  * @author sundaramtiwari  *  */ public class MultiThreadAbc { /** Configure number of threads counting number series */ private static final int NUM_OF_THREADS = 3; private static final int MAX_VALUE = 54; private final int noOfThreads; private final MyCount counter; private final CyclicBarrier barrier; private final Thread[] threads; public MultiThreadAbc(int noOfThreads, int maxCounterValue) { this.noOfThreads = noOfThread...

Spiral Traversal of a 2 D matrix in java

Spiral traversal of matrix is a commonly asked interview question at preliminary levels now. Here is my attempt to solve it: package com.rivigo.main; /**  * @author Sundaram  *  */ public class SpiralTraversal { /** *  1    2   3   4 5    6   7   8 9   10  11  12 13  14  15  16 17  18  19  20 */ public static void main(String[] args) { int[][] input = new int[][] {{1,2,3,4,}, {5,6,7,8}, {9, 10, 11, 12}, {13, 14, 15, 16}, {17, 18, 19, 20}}; spiralTraversal(input); } public static void spiralTraversal (int[][] input) { if (input != null) { // Temp matrix to hold data and track traversal int[][] temp = new int[input.length][input[0].length]; travelRight(input, temp, 0, 0); } else { System.out.println("Input matrix is null!"); } } public static void travelRight(int[][] input, int[][] temp, int row, int col...

Decimal Representation of a number (comma separated) in java

package challenges; import java.util.Stack; public class DecimalRepresentation { public static void main(String[] args) { System.out.println(decimalRepresent(123456789)); } private static String decimalRepresent(int no) { String noStr = Integer.toString(no); char[] charArr = noStr.toCharArray(); StringBuffer sbr = new StringBuffer(); Stack<Character> stack = new Stack<Character>(); int pos = 0; for (int i=charArr.length-1; i>=0; i--) { if (pos == 3) { stack.push(','); pos = 0; } else { pos++; stack.push(charArr[i]); } } while(!stack.isEmpty()) { sbr.append(stack.pop()); } return sbr.toString(); } }

Print Even and Odd Numbers using two threads in java

Frequently asked multi-threading interview question in java is printing odd numbers by one thread and even numbers by another thread in ascending order. Interviewer wants to check if you are able to implement thread communication properly. package thread; public class EvenOddByThreads { public static void main(String[] args) { Object obj = new Object(); Thread odd = new Thread(new EvenOddThread(obj, 1)); Thread even = new Thread(new EvenOddThread(obj, 2)); odd.start(); even.start(); } } class EvenOddThread implements Runnable { private Object lock; private int start; public EvenOddThread(Object lock, int start){ this.lock = lock; this.start = start; } @Override public void run() { synchronized (lock) { for (int i=start; i<=20; i+=2) { try { System.out.println(i); lock.notify(); lock.wait(); } catch (Exception e) { } } } } }