Aug 28, 2011

Important Thread Concepts

Thread Synchronization:
            When two or more threads need access to a shared resource, they need some way to ensure that the resource will be used by only one thread at a time.
The process by which this synchronization is achieved is called thread synchronization.
The synchronized keyword in Java creates a block of code referred to as a critical section. Every Java object with a critical section of code gets a lock associated with the object. To enter a critical section, a thread needs to obtain the corresponding object's lock.
This is the general form of the synchronized statement:

synchronized(object) {
   // statements to be synchronized
}


Here, object is a reference to the object being synchronized. A synchronized block ensures that a call to a method that is a member of object occurs only after the current thread has successfully entered object's monitor.
Here is an example, using a synchronized block within the run( ) method:

// File Name : Callme.java
// This program uses a synchronized block.
class Callme {
   void call(String msg) {
      System.out.print("[" + msg);
      try {
         Thread.sleep(1000);
      } catch (InterruptedException e) {
         System.out.println("Interrupted");
      }
      System.out.println("]");
   }
}

// File Name : Caller.java
class Caller implements Runnable {
   String msg;
   Callme target;
   Thread t;
   public Caller(Callme targ, String s) {
      target = targ;
      msg = s;
      t = new Thread(this);
      t.start();
   }
  
   // synchronize calls to call()
   public void run() {
      synchronized(target) { // synchronized block
         target.call(msg);
      }
   }
}
// File Name : Synch.java
class Synch {
   public static void main(String args[]) {
      Callme target = new Callme();
      Caller ob1 = new Caller(target, "Hello");
      Caller ob2 = new Caller(target, "Synchronized");
      Caller ob3 = new Caller(target, "World");
  
      // wait for threads to end
      try {
         ob1.t.join();
         ob2.t.join();
         ob3.t.join();
      } catch(InterruptedException e) {
         System.out.println("Interrupted");
      }
   }
}

Output:

[Hello]
[World]
[Synchronized]

Inter Thread Communication
           Consider the classic queuing problem, where one thread is producing some data and another is consuming it. To make the problem more interesting, suppose that the producer has to wait until the consumer is finished before it generates more data.
In a polling system, the consumer would waste many CPU cycles while it waited for the producer to produce. Once the producer was finished, it would start polling, wasting more CPU cycles waiting for the consumer to finish, and so on. Clearly, this situation is undesirable.
To avoid polling, Java includes an elegant interprocess communication mechanism via the following methods:
•    wait( ): This method tells the calling thread to give up the monitor and go to sleep until some other thread enters the same monitor and calls notify( ).
•    notify( ): This method wakes up the first thread that called wait( ) on the same object.
•    notifyAll( ): This method wakes up all the threads that called wait( ) on the same object.c The highest priority thread will run first.
These methods are implemented as final methods in Object, so all classes have them. All three methods can be called only from within a synchronized context.
These methods are declared within Object. Various forms of wait( ) exist that allow you to specify a period of time to wait.
 
Example:
The following sample program consists of four classes: Q, the queue that you're trying to synchronize; Producer, the threaded object that is producing queue entries; Consumer, the threaded object that is consuming queue entries; and PC, the tiny class that creates the single Q, Producer, and Consumer.
The proper way to write this program in Java is to use wait( ) and notify( ) to signal in both directions, as shown here:

class Q {
   int n;
   boolean valueSet = false;
   synchronized int get() {
      if(!valueSet)
      try {
         wait();
      } catch(InterruptedException e) {
         System.out.println("InterruptedException caught");
      }
      System.out.println("Got: " + n);
      valueSet = false;
      notify();
      return n;
   }

   synchronized void put(int n) {
      if(valueSet)
      try {
         wait();
      } catch(InterruptedException e) {
         System.out.println("InterruptedException caught");
      }
      this.n = n;
      valueSet = true;
      System.out.println("Put: " + n);
      notify();
   }
}

class Producer implements Runnable {
   Q q;
   Producer(Q q) {
      this.q = q;
      new Thread(this, "Producer").start();
   }

   public void run() {
      int i = 0;
      while(true) {
         q.put(i++);
      }
   }
}

class Consumer implements Runnable {
    Q q;
    Consumer(Q q) {
       this.q = q;
       new Thread(this, "Consumer").start();
    }
    public void run() {
       while(true) {
       q.get();
    }
  }
}
class PCFixed {
   public static void main(String args[]) {
      Q q = new Q();
      new Producer(q);
      new Consumer(q);
      System.out.println("Press Control-C to stop.");
   }
}

Inside get( ), wait( ) is called. This causes its execution to suspend until the Producer notifies you that some data is ready.
When this happens, execution inside get( ) resumes. After the data has been obtained, get( ) calls notify( ). This tells Producer that it is okay to put more data in the queue.
Inside put( ), wait( ) suspends execution until the Consumer has removed the item from the queue. When execution resumes, the next item of data is put in the queue, and notify( ) is called. This tells the Consumer that it should now remove it.
Here is some output from this program, which shows the clean synchronous behavior:

Put: 1
Got: 1
Put: 2
Got: 2
Put: 3
Got: 3
Put: 4
Got: 4
Put: 5
Got: 5


Thread DeadLock:
            A special type of error that you need to avoid that relates specifically to multitasking is deadlock, which occurs when two threads have a circular dependency on a pair of synchronized objects.
For example, suppose one thread enters the monitor on object X and another thread enters the monitor on object Y. If the thread in X tries to call any synchronized method on Y, it will block as expected. However, if the thread in Y, in turn, tries to call any synchronized method on X, the thread waits forever, because to access X, it would have to release its own lock on Y so that the first thread could complete.
 
Example:
     To understand deadlock fully, it is useful to see it in action. The next example creates two classes, A and B, with methods foo( ) and bar( ), respectively, which pause briefly before trying to call a method in the other class.
The main class, named Deadlock, creates an A and a B instance, and then starts a second thread to set up the deadlock condition. The foo( ) and bar( ) methods use sleep( ) as a way to force the deadlock condition to occur.

class A {
   synchronized void foo(B b) {
      String name = Thread.currentThread().getName();
      System.out.println(name + " entered A.foo");
      try {
         Thread.sleep(1000);
      } catch(Exception e) {
         System.out.println("A Interrupted");
      }
      System.out.println(name + " trying to call B.last()");
      b.last();
   }
   synchronized void last() {
      System.out.println("Inside A.last");
   }
}
class B {
   synchronized void bar(A a) {
      String name = Thread.currentThread().getName();
      System.out.println(name + " entered B.bar");
      try {
         Thread.sleep(1000);
      } catch(Exception e) {
         System.out.println("B Interrupted");
      }
      System.out.println(name + " trying to call A.last()");
      a.last();
   }
   synchronized void last() {
      System.out.println("Inside A.last");
   }
}
class Deadlock implements Runnable {
   A a = new A();
   B b = new B();
   Deadlock() {
      Thread.currentThread().setName("MainThread");
      Thread t = new Thread(this, "RacingThread");
      t.start();
      a.foo(b); // get lock on a in this thread.
      System.out.println("Back in main thread");
   }
   public void run() {
      b.bar(a); // get lock on b in other thread.
      System.out.println("Back in other thread");
   }
   public static void main(String args[]) {
      new Deadlock();
   }
}

Output:

MainThread entered A.foo
RacingThread entered B.bar
MainThread trying to call B.last()
RacingThread trying to call A.last()

Because the program has deadlocked, you need to press CTRL-C to end the program. You can see a full thread and monitor cache dump by pressing CTRL-BREAK on a PC .
You will see that RacingThread owns the monitor on b, while it is waiting for the monitor on a. At the same time, MainThread owns a and is waiting to get b. This program will never complete.
As this example illustrates, if your multithreaded program locks up occasionally, deadlock is one of the first conditions that you should check for.
Ordering Locks:
Acommon threading trick to avoid the deadlock is to order the locks. By ordering the locks, it gives threads a specific order to obtain multiple locks.
 
Deadlock Example:
Following is the depiction of a dead lock:

// File Name ThreadSafeBankAccount.java
public class ThreadSafeBankAccount
{
   private double balance;
   private int number;
   public ThreadSafeBankAccount(int num, double initialBalance)
   {
      balance = initialBalance;
      number = num;
   }
   public int getNumber()
   {
      return number;
   }
   public double getBalance()
   {
      return balance;
   }
   public void deposit(double amount)
   {
      synchronized(this)
      {
        double prevBalance = balance;
        try
        {
           Thread.sleep(4000);
        }catch(InterruptedException e)
        {}
        balance = prevBalance + amount;
      }
   }
   public void withdraw(double amount)
   {
      synchronized(this)
      {
         double prevBalance = balance;
         try
         {
            Thread.sleep(4000);
         }catch(InterruptedException e)
         {}
         balance = prevBalance - amount;
      }
   }
}

// File Name LazyTeller.java
public class LazyTeller extends Thread
{
   private ThreadSafeBankAccount source, dest;
   public LazyTeller(ThreadSafeBankAccount a,
                     ThreadSafeBankAccount b)
   {
      source = a;
      dest = b;
   }
   public void run()
   {
      transfer(250.00);
   }
   public void transfer(double amount)
   {
      System.out.println("Transferring from "
          + source.getNumber() + " to " + dest.getNumber());
      synchronized(source)
      {
          Thread.yield();
          synchronized(dest)
          {
             System.out.println("Withdrawing from "
                     + source.getNumber());
             source.withdraw(amount);
             System.out.println("Depositing into "
                     + dest.getNumber());
             dest.deposit(amount);
          }
       }
   }
}
public class DeadlockDemo
{
   public static void main(String [] args)
   {
      System.out.println("Creating two bank accounts...");
      ThreadSafeBankAccount checking =
                    new ThreadSafeBankAccount(101, 1000.00);
      ThreadSafeBankAccount savings =
                    new ThreadSafeBankAccount(102, 5000.00);

      System.out.println("Creating two teller threads...");
      Thread teller1 = new LazyTeller(checking, savings);
      Thread teller2 = new LazyTeller(savings, checking);
      System.out.println("Starting both threads...");
      teller1.start();
      teller2.start();
   }
}

This would produce following result:

Creating two bank accounts...
Creating two teller threads...
Starting both threads...
Transferring from 101 to 102
Transferring from 102 to 101

The problem with the LazyTeller class is that it does not consider the possibility of a race condition, a common occurrence in multithreaded programming.
After the two threads are started, teller1 grabs the checking lock and teller2 grabs the savings lock. When teller1 tries to obtain the savings lock, it is not available. Therefore, teller1 blocks until the savings lock becomes available. When the teller1 thread blocks, teller1 still has the checking lock and does not let it go.
Similarly, teller2 is waiting for the checking lock, so teller2 blocks but does not let go of the savings lock. This leads to one result: deadlock!
 
Deadlock Solution Example:
        Here transfer() method, in a class named OrderedTeller, in stead of arbitrarily synchronizing on locks, this transfer() method obtains locks in a specified order based on the number of the bank account.


// File Name ThreadSafeBankAccount.java
public class ThreadSafeBankAccount
{
   private double balance;
   private int number;
   public ThreadSafeBankAccount(int num, double initialBalance)
   {
      balance = initialBalance;
      number = num;
   }
   public int getNumber()
   {
      return number;
   }
   public double getBalance()
   {
      return balance;
   }
   public void deposit(double amount)
   {
      synchronized(this)
      {
        double prevBalance = balance;
        try
        {
           Thread.sleep(4000);
        }catch(InterruptedException e)
        {}
        balance = prevBalance + amount;
      }
   }
   public void withdraw(double amount)
   {
      synchronized(this)
      {
         double prevBalance = balance;
         try
         {
            Thread.sleep(4000);
         }catch(InterruptedException e)
         {}
         balance = prevBalance - amount;
      }
   }
}
// File Name OrderedTeller.java
public class OrderedTeller extends Thread
{
   private ThreadSafeBankAccount source, dest;
   public OrderedTeller(ThreadSafeBankAccount a,
                        ThreadSafeBankAccount b)
   {
      source = a;
      dest = b;
   }
   public void run()
   {
      transfer(250.00);
   }
   public void transfer(double amount)
   {
       System.out.println("Transferring from " + source.getNumber()
           + " to " + dest.getNumber());
       ThreadSafeBankAccount first, second;
       if(source.getNumber() < dest.getNumber())
       {
          first = source;
          second = dest;
       }
       else
       {
          first = dest;
          second = source;
       }
       synchronized(first)
       {
          Thread.yield();
          synchronized(second)
          {
             System.out.println("Withdrawing from "
                         + source.getNumber());
             source.withdraw(amount);
             System.out.println("Depositing into "
                         + dest.getNumber());
             dest.deposit(amount);
          }
      }
   }
}

// File Name DeadlockDemo.java
public class DeadlockDemo
{
   public static void main(String [] args)
   {
      System.out.println("Creating two bank accounts...");
      ThreadSafeBankAccount checking =
                    new ThreadSafeBankAccount(101, 1000.00);
      ThreadSafeBankAccount savings =
                    new ThreadSafeBankAccount(102, 5000.00);

      System.out.println("Creating two teller threads...");
      Thread teller1 = new OrderedTeller(checking, savings);
      Thread teller2 = new OrderedTeller(savings, checking);
      System.out.println("Starting both threads...");
      teller1.start();
      teller2.start();
   }
}

This would remove deadlock problem and would produce following result:

Creating two bank accounts...
Creating two teller threads...
Starting both threads...
Transferring from 101 to 102
Transferring from 102 to 101
Withdrawing from 101
Depositing into 102
Withdrawing from 102
Depositing into 101

Thread Control: suspend, stop and resume methods
          While the suspend( ), resume( ), and stop( ) methods defined by Thread class seem to be a perfectly reasonable and convenient approach to managing the execution of threads, they must not be used for new Java programs and obsolete in newer versions of Java.
The following example illustrates how the wait( ) and notify( ) methods that are inherited from Object can be used to control the execution of a thread.
This example is similar to the program in the previous section. However, the deprecated method calls have been removed. Let us consider the operation of this program.
The NewThread class contains a boolean instance variable named suspendFlag, which is used to control the execution of the thread. It is initialized to false by the constructor.
The run( ) method contains a synchronized statement block that checks suspendFlag. If that variable is true, the wait( ) method is invoked to suspend the execution of the thread. The mysuspend( ) method sets suspendFlag to true. The myresume( ) method sets suspendFlag to false and invokes notify( ) to wake up the thread. Finally, the main( ) method has been modified to invoke the mysuspend( ) and myresume( ) methods.
 
Example:
// Suspending and resuming a thread for Java 2
class NewThread implements Runnable {
   String name; // name of thread
   Thread t;
   boolean suspendFlag;
   NewThread(String threadname) {
      name = threadname;
      t = new Thread(this, name);
      System.out.println("New thread: " + t);
      suspendFlag = false;
      t.start(); // Start the thread
   }
   // This is the entry point for thread.
   public void run() {
      try {
      for(int i = 15; i > 0; i--) {
         System.out.println(name + ": " + i);
         Thread.sleep(200);
         synchronized(this) {
            while(suspendFlag) {
               wait();
            }
          }
        }
      } catch (InterruptedException e) {
         System.out.println(name + " interrupted.");
      }
      System.out.println(name + " exiting.");
   }
   void mysuspend() {
      suspendFlag = true;
   }
   synchronized void myresume() {
      suspendFlag = false;
       notify();
   }
}

class SuspendResume {
   public static void main(String args[]) {
      NewThread ob1 = new NewThread("One");
      NewThread ob2 = new NewThread("Two");
      try {
         Thread.sleep(1000);
         ob1.mysuspend();
         System.out.println("Suspending thread One");
         Thread.sleep(1000);
         ob1.myresume();
         System.out.println("Resuming thread One");
         ob2.mysuspend();
         System.out.println("Suspending thread Two");
         Thread.sleep(1000);
         ob2.myresume();
         System.out.println("Resuming thread Two");
      } catch (InterruptedException e) {
         System.out.println("Main thread Interrupted");
      }
      // wait for threads to finish
      try {
         System.out.println("Waiting for threads to finish.");
         ob1.t.join();
         ob2.t.join();
      } catch (InterruptedException e) {
         System.out.println("Main thread Interrupted");
      }
      System.out.println("Main thread exiting.");
   }
}

Output:

New thread: Thread[One,5,main]
One: 15
New thread: Thread[Two,5,main]
Two: 15
One: 14
Two: 14
One: 13
Two: 13
One: 12
Two: 12
One: 11
Two: 11
Suspending thread One
Two: 10
Two: 9
Two: 8
Two: 7
Two: 6
Resuming thread One
Suspending thread Two
One: 10
One: 9
One: 8
One: 7
One: 6
Resuming thread Two
Waiting for threads to finish.
Two: 5
One: 5
Two: 4
One: 4
Two: 3
One: 3
Two: 2
One: 2
Two: 1
One: 1
Two exiting.
One exiting.
Main thread exiting.

*Using Multithreading:
          The key to utilizing multithreading support effectively is to think concurrently rather than serially. For example, when you have two subsystems within a program that can execute concurrently, make them individual threads.
With the careful use of multithreading, you can create very efficient programs. A word of caution is in order, however: If you create too many threads, you can actually degrade the performance of your program rather than enhance it.
Remember, some overhead is associated with context switching. If you create too many threads, more CPU time will be spent changing contexts than executing your program!

0 comments :

Post a Comment