Tuesday, December 2, 2014

Write a program for deadlock


class Resource1 extends Thread {
  public void run() {
    System.out.println("Resource1" + getName());
    synchronized (Resource1.class) {
      try {
        Thread.sleep(10);
      }
      catch (InterruptedException e) {
        e.printStackTrace();
      }
      synchronized (Resource2.class) {
      }
    }
  }
}
class Resource2 extends Thread {
  public void run() {
    System.out.println("Resource2" + getName());
    synchronized (Resource2.class) {
      try {
        Thread.sleep(10);
      }
      catch (InterruptedException e) {
        e.printStackTrace();
      }
      synchronized (Resource1.class) {
      }
    }
  }
}
public class Deadlock_example {
  public static void main(String[] args) {
    Resource1 r1 = new Resource1();
    Resource2 r2 = new Resource2();
    r1.start();
    r2.start();
  }
}

Count the number of chars in string..

public class Countnumber {
  public static void main(String args[]){
    String s = "aabbbccccdddddeeeeeeffffffffff";
    Map<Character, Integer> m =new HashMap<Character, Integer>();
    char c;
   
    int count = 1;
    for(int i= 0;i<s.length();i++){
     
      c = s.charAt(i);
      if(m.containsKey(c)){
        count = (int) m.get(c);
        count++;
      }else{
        count = 1;
      }
      m.put(c, count);
    }
    Iterator it = m.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry)it.next();
        System.out.println(pairs.getKey() + " = " + pairs.getValue());
    }
    for(Map.Entry<Character, Integer> entry : m.entrySet()){
      System.out.println(entry.getKey() + " = " + entry.getValue());
    }
  }
}
O/p  ::
f = 10
d = 5
e = 6
b = 3
c = 4
a = 2

Green Vs Native Threads

Green threads Will function on systems that don't support OS level threads.

Another advantage of native threads is that multiple processes would execute in parallel in a multi-core machine.
On the other hand, green threads are executed on a single core the entire time and it is the VM that gives a multi-threading notion.
So actually there is only singe executing in case of green threads.

Native threads uses OS scheduling algorithm. Modern day OSes support pre-emptive scheduling.
Green threads can use any kind of scheduling algorithm

Native threads usually have complicated synchronization and resource sharing. Multiple processes would require kernel level locks for synchronization.
Synchronizations are pretty easier in case of green threads.

For purely sequential code, green threads are slightly faster than native threads.

For multithreaded code that involves frequent synchronization, green threads are likely to be faster than native threads.
Methods such as Object.notify() are much faster for green threads as compared to native threads.

Green threads Cannot support multi-processor machines

Green threads are generally a bit more "lightweight" than native threads

Green threads are scales to large numbers of threads than native threads.

Future Methods

A future method runs in the background, asynchronously. You can call a future method for executing long-running operations, such as callouts to external Web services or any operation you’d like to run in its own thread, on its own time. 

To define a future method, simply annotate it with the future annotation, as follows.
global class FutureClass
{
    @future
    public static void myFutureMethod()
    {   
         // Perform some operations
    }
}

The difference between the Runnable and Callable interfaces in Java..

  • A Callable needs to implement call() method while a Runnable needs to implement run() method.
  • A Callable can return a value but a Runnable cannot.
  • A Callable can throw checked exception but a Runnable cannot.
  • A Callable can be used with ExecutorService#invokeXXX methods but a Runnable cannot be.
    public interface Runnable {
        void run();
    }
    
    public interface Callable<V> {
        V call() throws Exception;
    }
     
    NOTE : Inside your class you can make the calls to Callable and Runnable
     on a single thread executor, making this mechanism similar to a serial 
    dispatch queue. So as long as the caller calls your Runnable wrapped 
    methods the calling thread will execute really fast without blocking. As
     soon as it calls a Callable wrapped in Future method it will have to 
    block till all the other queued items are executed. Only then the method
     will return with values. This is a synchronization mechanism.