Saturday, 11 January 2014

Advanced Java_ Multi-threading Part 11 - Deadlock




==
package Demo13;

/**
 * Title -- Advanced Java_ Multi-threading Part 11 - Deadlock
 *
 * @author Dharmaraj.Net
 */
public class App {

    public static void main(String[] args) throws Exception{
        final Runner runner = new Runner();
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                try {
                    runner.firstThread();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread t2 = new Thread(new Runnable() {
            public void run() {
                try {
                    runner.secondThread();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();
       
        runner.finished();
    }

}
============
package Demo13;

import java.util.Random;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Runner {

    private Account acc1 = new Account();
    private Account acc2 = new Account();

    private Lock lock1 = new ReentrantLock();
    private Lock lock2 = new ReentrantLock();

    private void acquireLock(Lock firstLock, Lock secondLock) throws InterruptedException {
        while(true){
            //Acquire lock
            boolean getFirstLock = false;
            boolean getSecondLock = false;
           
           
            try {
                getFirstLock =  firstLock.tryLock();
                getSecondLock = secondLock.tryLock();
            } finally{
                if(getFirstLock && getSecondLock)
                {
                    return;
                }
               
                if(getFirstLock){
                    firstLock.unlock();
                }
                if(getSecondLock){
                    secondLock.unlock();
                }
            }
           
            //Locks not acquire
            Thread.sleep(1);
        }
    }

    public void firstThread() throws InterruptedException {
        Random random = new Random();

        for (int i = 0; i < 10000; i++) {
            acquireLock(lock1, lock2);
            lock2.lock();
            try {
                Account.transfer(acc1, acc2, random.nextInt(100));
            } finally {
                lock1.unlock();
                lock2.unlock();
            }
        }
    }

    public void secondThread() throws InterruptedException {
        Random random = new Random();

        for (int i = 0; i < 10000; i++) {
            acquireLock(lock2, lock1);
            try {
                Account.transfer(acc2, acc1, random.nextInt(100));
            } finally {
                lock1.unlock();
                lock2.unlock();
            }
        }
    }

    public void finished() {
        System.out.println("Account 1 Balance " + acc1.getBalance());
        System.out.println("Account 2 Balance " + acc2.getBalance());
        System.out.println("Total Balance "
                + (acc1.getBalance() + acc2.getBalance()));

    }
}
==========
package Demo13;

public class Account {
    private int balance = 10000;

    public void deposit(int amount) {
        balance += amount;
    }

    public void withdraw(int amount) {
        balance -= amount;
    }

    public static void transfer(Account acc1, Account acc2, int amount) {
        acc1.withdraw(amount);
        acc2.deposit(amount);
    }

    public int getBalance() {
        return balance;
    }
========

Advanced Java_ Multi-threading Part 10 - Re-entrant Locks


===
package Demo12;

import Demo11.Processor;

/**
 * Title -- Advanced Java_ Multi-threading Part 10 - Re-entrant Locks
 *
 * @author Dharmaraj.Net
 */
public class App {

    public static void main(String[] args) throws Exception{
        final Runner runner = new Runner();

        Thread t1 = new Thread(new Runnable() {

            public void run() {
                try {
                    runner.firstThread();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        });

        Thread t2 = new Thread(new Runnable() {

            public void run() {
                try {
                    runner.secondThread();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();
       
        runner.finished();
    }

}
===========
package Demo12;

import java.util.Date;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Runner {

    private int count = 0;
    // ReentrantLock Means once any thread hold the lock of ReentrantLock will
    // count how many time it take the lock and need to unlock the same time
    private Lock lock = new ReentrantLock();
    private Condition cond = lock.newCondition();

    private void increment() {
        for (int i = 0; i < 10000; i++) {
            count++;
        }
    }

    public void firstThread() throws InterruptedException {
        lock.lock();
        System.out.println("Waiting..");
        cond.await();
        System.out.println("Worken up..");
        try {
            increment();
        } finally {
            lock.unlock();
        }

    }

    public void secondThread() throws InterruptedException {
        Thread.sleep(1000);
        lock.lock();
        System.out.println("Press the return key...");
        new Scanner(System.in).nextLong();
        System.out.println("Got return key...");
        cond.signal();
        try {
            increment();
        } finally {
            lock.unlock();
        }
    }

    public void finished() {
        System.out.println("Count is " + count);
    }
}
=========
 

Advanced Java_ Multi-threading Part 8 - Wait and Notify


==
package Demo10;

/**
 * Title -- Advanced Java_ Multi-threading Part 8 - Wait and Notify
 * 
 * @author Dharmaraj.Net
 */
public class App {

 public static void main(String[] args) throws InterruptedException {
  final Processor processor = new Processor();

  Thread t1 = new Thread(new Runnable() {

   public void run() {
    try {
     processor.producer();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }

  });

  Thread t2 = new Thread(new Runnable() {

   public void run() {
    try {
     processor.consumer();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }

  });

  t1.start();
  t2.start();

  t1.join();
  t2.join();
 }

}
=====
package Demo10;

import java.util.Scanner;

public class Processor {
 public void producer() throws InterruptedException {
  synchronized (this) {
   System.out.println("Producer thread running ......");
   wait();
   System.out.println("Resumed ......");
  }
 }

 public void consumer() throws InterruptedException {
  Scanner scanner = new Scanner(System.in);
  Thread.sleep(2000);
  
  synchronized (this) {
   System.out.println("Waiting for return key");
   scanner.nextLine();
   System.out.println("Return key pressed");
   notify();
  }
 }
} 

Advanced Java_ Multi-threading Part 9 - A Worked Example Using Low-Level Synchronization


====
package Demo11;

/**
 * Title -- Advanced Java_ Multi-threading Part 9 - A Worked Example Using Low-Level Synchronization
 *
 * @author Dharmaraj.Net
 */
public class App {

 public static void main(String[] args) throws InterruptedException {
  final Processor processor = new Processor();

  Thread t1 = new Thread(new Runnable() {

   public void run() {
    try {
     processor.producer();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }

  });

  Thread t2 = new Thread(new Runnable() {

   public void run() {
    try {
     processor.consumer();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }

  });

  t1.start();
  t2.start();

  t1.join();
  t2.join();
 }

}
====
package Demo11;

import java.util.LinkedList;
import java.util.Random;

public class Processor {

 private LinkedList<Integer> list = new LinkedList<Integer>();
 private final int LIMIT = 10;
 private Object lock = new Object();

 /**
  * This method is add items to the list
  * 
  * @throws InterruptedException
  */
 public void producer() throws InterruptedException {
  int value = 0;
  while (true) {

   synchronized (lock) {

    while (list.size() == LIMIT) {
     lock.wait();
    }

    list.add(value++);
    lock.notify();
   }
  }

 }

 /**
  * This method is remove items to the list
  * 
  * @throws InterruptedException
  */
 public void consumer() throws InterruptedException {
  Random random = new Random();
  while (true) {
   synchronized (lock) {
    while (list.size() == 0) {
     lock.wait();
    }
    System.out.println("List Size " + list.size());
    int value = list.removeFirst();
    System.out.println("Value is " + value);
    lock.notify();
   }
   Thread.sleep(random.nextInt(1000));
  }
 }
} 

Advanced Java_ Multi-threading Part 6 -- Countdown Latches


==
package Demo8;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Title -- Advanced Java_ Multi-threading Part 6 -- Countdown Latches
 * 
 * @author Dharmaraj.Net
 */
public class App {

 public static void main(String[] args) {
  CountDownLatch latch = new CountDownLatch(3);
  ExecutorService executor = Executors.newFixedThreadPool(3);
  for (int i = 0; i < 3; i++) {
   executor.submit(new Processor(latch));
  }
  try {
   latch.await();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  System.out.println("Completed");
 }

}

class Processor implements Runnable {

 private CountDownLatch latch;

 public Processor(CountDownLatch latch) {
  this.latch = latch;
 }

 public void run() {
  System.out.println("Started....");
  
  try {
   Thread.sleep(3000);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  
  latch.countDown();
 }

}

Advanced Java_ Multi-threading Part 7 - Producer-Consumer


==
package Demo9;

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/**
 * Title -- Advanced Java_ Multi-threading Part 7 - Producer-Consumer
 * 
 * @author Dharmaraj.Net
 */
public class App {
 private static BlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(10);

 public static void main(String[] args) throws InterruptedException {
  Thread t1 = new Thread(new Runnable(){

   public void run() {
    try {
     producer();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
   
  });
  
  Thread t2 = new Thread(new Runnable(){

   public void run() {
    try {
     consumer();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
   
  });
  
  t1.start();
  t2.start();
  
  t1.join();
  t2.join();
 }

 private static void producer() throws InterruptedException {
  Random random = new Random();

  while (true) {
   queue.put(random.nextInt(100));
  }
 }

 private static void consumer() throws InterruptedException {
  Random random = new Random();
  while (true) {
   Thread.sleep(100);

   if (random.nextInt(10) == 0) {
    Integer value = queue.take();

    System.out.println("Taken Value = " + value + "Queue Size is = "
      + queue.size());
   }
  }
 }
}
==== 

Advanced Java_ Multi-threading Part 5 -- Thread Pools


==
package Demo7;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Title -- Advanced Java_ Multi-threading Part 5 -- Thread Pools Note:--
 * 
 * @author Dharmaraj.Net
 */
public class App {
 public static void main(String[] args) {
  ExecutorService executor = Executors.newFixedThreadPool(2); // Two
                 // factor
                 // workers
                 // two
                 // threads
  for (int i = 0; i < 5; i++) {
   executor.submit(new processor(i));
  }
  executor.shutdown();
  System.out.println("All task submited");
  try {
   executor.awaitTermination(1, TimeUnit.DAYS);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  
  System.out.println("All task completed");
 }
}

class processor implements Runnable {

 private int id;

 public processor(int id) {
  this.id = id;
 }

 public void run() {
  System.out.println("Starting :" + id);
  try {
   Thread.sleep(5000);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  System.out.println("Complet :" + id);
 }

}

Advanced Java_ Multi-threading Part 4 -- Multiple Locks; Using Synchronized Code Blocks


==
package Demo6;
/**
 * Title -- Advanced Java_ Multi-threading Part 4 -- Multiple Locks; Using Synchronized Code Blocks
 * Note:-- 
 * @author Dharmaraj.Net
 */
public class App {

 public static void main(String[] args) {
  new Worker().main();
 }

}
===
package Demo6;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Worker {

 private List<Integer> list1 = new ArrayList<Integer>();
 private List<Integer> list2 = new ArrayList<Integer>();

 private Object lock1 = new Object();
 private Object lock2 = new Object();

 private Random random = new Random();

 public void main() {
  System.out.println("Starting.....");

  long start = System.currentTimeMillis();
  // process();

  Thread t1 = new Thread(new Runnable() {
   public void run() {
    process();
   }
  });
  t1.start();

  Thread t2 = new Thread(new Runnable() {
   public void run() {
    process();
   }
  });
  t2.start();

  try {
   t1.join();
   t2.join();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }

  long end = System.currentTimeMillis();

  System.out.println("Time taken..." + (end - start));
  System.out.println("Liat 1" + list1.size());
  System.out.println("Liat 1" + list2.size());
 }

 public void stageOne() {
  synchronized (lock1) {
   try {
    Thread.sleep(1);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   list1.add(random.nextInt(100));
  }
 }

 public void stageTwo() {
  synchronized (lock2) {
   try {
    Thread.sleep(1);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   list2.add(random.nextInt(100));
  }
 }

 public void process() {
  for (int i = 0; i < 1000; i++) {
   stageOne();
   stageTwo();
  }
 }

}
=== 

Advanced Java_ Multi-threading Part 2 -- Basic Thread Synchronization - 3


===
package Demo4;

import java.util.Scanner;

/**
 * Title:- Advanced Java_ Multi-threading Part 2 -- Basic Thread Synchronization - 3
 * Note:-- Basic Thread synchronization
 * @author Dharmaraj.Net
 * There are two kind of problem encounter when thread sharing same data
 * First one is data being cashed abd secibd thread leaving
 * Propose of volatile key word in java and basic thread synchronization
 * 
 */

class Processor extends Thread{
 private boolean running = true;
 public void run(){
  while(true){
   System.out.println("Hello");
   try {
    Thread.sleep(100);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }
 
 public void shotDown()
 {
  running = false;
 }
}

public class App {

 public static void main(String[] args) {
  Processor proc1 = new Processor();
  proc1.start();
  
  System.out.println("Press return to Stop....");
  Scanner scanner = new Scanner(System.in);
  scanner.nextLine();
  
  
 }
}
=== 

Advanced Java_ Multi-threading Part 3 -- The Synchronized Keyword


==
package Demo5;

/**
 * Title -- Advanced Java_ Multi-threading Part 3 -- The Synchronized Keyword
 * Note:-- Basic Thread synchronization
 * @author Bapa
 * Every object in java has a intransit lock one thread can aquare at a time lock of an object.
 */
public class App {

 private int count = 0;

 public synchronized void increement() {
  count++;
 }

 public static void main(String[] args) {
  App app = new App();
  app.doWork();
 }

 public void doWork() {
  Thread t1 = new Thread(new Runnable() {
   public void run() {
    for (int i = 0; i < 10000; i++) {
     increement();
    }
   }

  });

  Thread t2 = new Thread(new Runnable() {
   public void run() {
    for (int i = 0; i < 10000; i++) {
     increement();
    }
   }

  });

  t1.start();
  t2.start();

  try {
   t1.join();
   t2.join();
  } catch (InterruptedException e) {
   e.printStackTrace();
  }

  System.out.println("Count is " + count);
 }

}

Advanced Java_ Multi-threading Part 2 -- Basic Thread Synchronization - 1


==
package Demo2;
/**
 * Title:- Advanced Java_ Multi-threading Part 2 -- Basic Thread Synchronization - 1
 * @author Dharmaraj.Net
 * This example shows how implements Runnable interface and basic work of thread and run and start method
 */
class Runner implements Runnable {

 public void run() {
  for (int i = 0; i < 10; i++) {
   System.out.println("Hello" + "=" + i);

   try {
    Thread.sleep(100);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }

}

public class App {

 public static void main(String[] args) {
  Thread t1 = new Thread(new Runner());
  Thread t2 = new Thread(new Runner());
  
  t1.start();
  t2.start();
  
 }

}

Advanced Java_ Multi-threading Part 2 -- Basic Thread Synchronization - 2


==
package Demo3;
/**
 * Title:- Advanced Java_ Multi-threading Part 2 -- Basic Thread Synchronization - 2
 * @author Dharmaraj.Net
 * This example shows how implements Runnable interface execute your thread
 */
public class App {

 public static void main(String[] args) {
  Thread t1 = new Thread(new Runnable(){
   public void run() {
    for (int i = 0; i < 10; i++) {
     System.out.println("Hello" + "=" + i);

     try {
      Thread.sleep(100);
     } catch (InterruptedException e) {
      e.printStackTrace();
     }
    }
   }
  });
  t1.start();
 }

}

Advanced Java_ Multi-threading Part 1 -- Starting Threads


==
package Demo1;

/**
 * Title:--Advanced Java_ Multi-threading Part 1 -- Starting Threads
 * 
 * @author Dharmaraj.Net This example shows how thread extend and basic work of thread
 *         and run and start method
 */

class Runner extends Thread {

 private String myString;

 public Runner(String myString) {
  this.myString = myString;
 }

 public void run() {
  for (int i = 0; i < 10; i++) {
   System.out.println(myString + "=" + i);

   try {
    Thread.sleep(100);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }

}

public class App {

 public static void main(String[] args) {
  Runner runner1 = new Runner("Hello One");
  runner1.start();

  Runner runner2 = new Runner("Hello Two");
  runner2.start();
 }

}