Friday, October 31, 2014

Java Thread III: Producer Consumer Using Synchronization vs Locks

Producer Consumer has always been favorite question in java multi-threading interviews.

We will create threads using two ways: Using usual synchronization & Locks.

Synchronization:\

Synchronization is standard way of locking shared resources and provide multi-threading. We  will create here two inner classes: Producer & Consumer and will use Stack to share the resources.

Producer:

class Producer extends Thread {


@Override
public void run() {
while (true) {
synchronized (itemStack) {
if (itemStack.size() <= MAX) {
COUNTER++;
itemStack.push(String.valueOf(COUNTER));
System.out.println("producing item ->" + COUNTER
+ " & size of stack is now :: "
+ itemStack.size());
itemStack.notifyAll();
}

while (itemStack.size() == MAX) {
try {
System.out.println("Stack is full & Producer is waiting.");
itemStack.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

}

Consumer:

private class Consumer extends Thread {


@Override
public void run() {
while (true) {
synchronized (itemStack) {
if (itemStack.size() != EMPTY) {
System.out.println("Consuming item ->"
+ itemStack.pop()
+ " & size of stack is now :: "
+ itemStack.size());
itemStack.notifyAll();
}g

while (itemStack.size() == EMPTY) {
System.out.println("Stack is empty & Consumer is waiting.");
COUNTER = 0;
try {
itemStack.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

}

As you can see here, we are trying to acquire the lock using synchronization on shared object Stack.

NOTE: Make sure you declare itemStack as static variable or else both threads will deadlock each other.

Working code can be found here.

Lock:

Now let's see how we can rewrite same example using ReentrantLock object which is provided in java.util.concurrent package in jdk 5.


Please see my earlier post on Reentrant Lock if you are new to Lock concepts.

To rewrite producer consumer example using Lock, we will need below piece of code.

    private static Lock lock = new ReentrantLock();
    private static Condition hasSpace = lock.newCondition();
    private static Condition hasItems = lock.newCondition();

First line will create explicit lock object for our example and rest of two lines will provide the conditions required to synchronize the Stack.

Condition objects provides await(), signal() & signalAll() methods similar to wait(), notify() and notifyAll() provided by Object class.

Now, Lets see how we can leverage all of this in our example below.

private class Producer extends Thread {

@Override
public void run() {
while (true) {
try {
lock.lock();
while (itemStack.size() >= MAX) {
System.out.println("Stack is full & Producer is waiting.");
try {
// wait for the list to have space
hasSpace.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

COUNTER++;
itemStack.push(String.valueOf(COUNTER));
System.out.println("producing item number -> "
+ itemStack.size());
hasItems.signalAll();

} finally {
lock.unlock();
}
}
}
}

private class Consumer extends Thread {

@Override
public void run() {
while (true) {
try {
lock.lock();
while (itemStack.size() == EMPTY) {
System.out.println("Stack is empty & Consumer is waiting.");
try {
COUNTER = 0;
// wait for the list to have space
hasItems.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
itemStack.pop();
System.out.println("consuming item number -> "+ itemStack.size());
hasSpace.signalAll();
} finally {
lock.unlock();
}
}
}

}

So As you can see now,  we are acquiring the lock explicitly at the beginning of run() method and releasing it in finally block. Also, lock object provides separate conditions for Producer & Consumer objects as well and simplifies the code further because you know here which condition should await and which one should be signaled. (Check synchronized version for wait & notify conditions to compare against).

It provides more readability to synchronization code since now you have separate object for locking and so you know what exactly you are synchronizing on. In synchronized version, remember, we were relying on Stack object for locking and sometime this can lead to more confusion and can create deadlocks as well. (Hint: try synchronized version with Stack object as non-static reference.)

However, here since locking is explicit, you need to be alert to unlock the lock object at the end of it can create issues later in the code.

Working code can be found here.

Hope, this will help you understand synchronization and locking concepts.

Thursday, October 30, 2014

Java Thread II: Reentrant Lock Vs Synchronized

Today we will see the difference between Lock and Synchronized block but before we go into details, let's understand what is Lock and how it works.

What is Lock ?

Lock interface was introduced in jdk 1.5 along with other concurrency utilities like countdown latch etc. ReentrantLock is the main concrete implementation of lock interface which behaves as mutually exclusive lock like synchronized block.
So the next question comes in mind is why, or most importantly when, one should prefer Lock when we already have synchronized. There are some trade offs between these two and Reentrant Lock can be used in couple of conditions.
For example, Lock provides explicit locking and so it has more readability comparing to synchronized. You will see this in below examples.

1. Fairness:

The ReentrantLock constructor offers a choice of two fairness options: create a non-fair lock or a fair lock. With fair locking, threads can acquire locks only in the order in which they were requested, whereas an unfair lock allows a lock to acquire it out of its turn. This is called barging (breaking the queue and acquiring the lock when it became available).

Fair locking has a significant performance cost because of the overhead of suspending and resuming threads. There could be cases where there is a significant delay between when a suspended thread is resumed and when it actually runs. Let's see a situation:

A -> holds a lock.
B -> has requested and is in a suspended state waiting for A to release the lock.
C -> requests the lock at the same time that A releases the lock, and has not yet gone to a suspended state.

As C has not yet gone to a suspended state, there is a chance that it can acquire the lock released by A, use it, and release it before B even finishes waking up. So, in this context, unfair lock has a significant performance advantage.

2. Polled and Timed Lock Acquisition: 

Let's see some example code:

public void transferMoneyWithSync(Account fromAccount, Account toAccount,
   float amount) throws InsufficientAmountException {
  synchronized (fromAccount) {
   // acquired lock on fromAccount Object
   synchronized (toAccount) {
    // acquired lock on toAccount Object
    if (amount > fromAccount.getCurrentAmount()) {
     throw new InsufficientAmountException(
       "Insufficient Balance");
    } else {
     fromAccount.debit(amount);
     toAccount.credit(amount);
    }
   }
  }
 }

In the transferMoney() method above, there is a possibility of deadlock when two threads 

A and B are trying to transfer money at almost the same time.
A: transferMoney(acc1, acc2, 20);
B: transferMoney(acc2, acc1 ,25);

It is possible that thread A has acquired a lock on the acc1 object and is waiting to acquire a lock on the acc2 object. Meanwhile, thread B has acquired a lock on the acc2 object and is waiting for a lock on acc1. This will lead to deadlock, and the system would have to be restarted! There is, however, a way to avoid this, which is called "lock ordering." Personally, I find this a bit complex. 

A cleaner approach is implemented by ReentrantLock with the use of tryLock() method. This approach is called the "timed and polled lock-acquisition." It lets you regain control if you cannot acquire all the required locks, release the ones you have acquired and retry. 

So, using tryLock, we will attempt to acquire both locks. If we cannot attain both, we will release if one of these has been acquired, then retry

public boolean transferMoneyWithTryLock(Account fromAccount,
   Account toAccount, float amount) throws InsufficientAmountException, InterruptedException 
{
 // we are defining a stopTime
 long stopTime = System.nanoTime() + 5000;
 while (true) {
  if (fromAccount.lock.tryLock()) {
    try {
   if (toAccount.lock.tryLock()) {
       try {
      if (amount > fromAccount.getCurrentAmount()) {
     throw new InsufficientAmountException(          "Insufficient Balance");
       } else {
     fromAccount.debit(amount);
     toAccount.credit(amount);
       }
    } finally {
       toAccount.lock.unlock();
    }
    }
      } finally {
   fromAccount.lock.unlock();
      }
  }
 if(System.nanoTime() < stopTime)
    return false;
    Thread.sleep(100);
 }//while
 }

Here we implemented a timed lock, so if the locks cannot be acquired within the specified time, the transferMoney method will return a failure notice and exit gracefully. We can also maintain time budget activities using this concept. 


3. Interruptible Lock Acquisition:

Interruptible lock acquisition allows locking to be used within cancellable activities.
The lockInterruptibly method allows us to try and acquire a lock while being available for interruption. So, basically it allows the thread to immediately react to the interrupt signal sent to it from another thread. 

This can be helpful when we want to send a KILL signal to all the waiting locks. Let's see one example: Suppose we have a shared line to send messages. We would want to design it in such a way that if another thread comes and interrupts the current thread, the lock should release and perform the exit or shut down operations to cancel the current task.

public boolean sendOnSharedLine(String message) throws InterruptedException{
  lock.lockInterruptibly();
  try{
   return cancellableSendOnSharedLine(message);
  } finally {
   lock.unlock();
  }
 }
private boolean cancellableSendOnSharedLine(String message){
.......

4. Non-block Structured Locking:

In intrinsic locks, acquire-release pairs are block-structured. In other words, a lock is always released in the same basic block in which it was acquired, regardless of how control exits the block. Extrinsic locks allow the facility to have more explicit control. Some concepts, like Lock Strapping, can be achieved more easily using extrinsic locks. Some use cases are seen in hash-bashed collections and linked lists.

private ReentrantLock lock;
public void foo() {
  ...
  lock.lock();
  ...
}
public void bar() {
  ...
  lock.unlock();
  ...
}

Intrinsic locks and extrinsic locks have the same mechanism inside for locking, so the performance improvement is purely subjective. It depends on the use cases we discussed above. Extrinsic locks give a more explicit control mechanism for better handling of deadlocks, starvation, and so on

When should you use ReentrantLocks? 

The answer is pretty simple - use it when you actually need something it provides that synchronized doesn't, like timed lock waits, interruptible lock waits, non-block-structured locks, multiple condition variables, or lock polling. ReentrantLock also has scalability benefits, and you should use it if you actually have a situation that exhibits high contention, but remember that the vast majority of synchronized blocks hardly ever exhibit any contention, let alone high contention. I would advise developing with synchronization until synchronization has proven to be inadequate, rather than simply assuming "the performance will be better" if you use ReentrantLock.

Remember, these are advanced tools for advanced users. (And truly advanced users tend to prefer the simplest tools they can find until they're convinced the simple tools are inadequate.) As always, make it right first, and then worry about whether or not you have to make it faster.


Friday, October 10, 2014

Java Basics: String Pool & String Handling in Java

In today's post we will  go through various ways of creating strings and understanding memory management of strings.

Java provides two ways of creating strings: using string literal or with new operator. So next question comes in mind is: why there are two different ways and what is the advantage of the same.

Before we go into more details, let's first understand internal memory management of String objects.

JVM maintains string pool to maintain all string variaobles in the memory. String pools are generally stored in the PermGen area of Heap memory since they are constants and will be required for longer period. So when you create string variable using literals, jvm first checks if variable already exists in the string pool. If it does then only reference of the object will be assigned to new string variable whereas when strings are created using new operator they are treated as any other java objects and will be stored in the heap memory.

So let's understand this concepts using below example,

   static String iAmReference = "i am reference"; 
   static String iAmSameReference = "i am reference"; 

Here, first variable, iAmReference will be created as new object since string doesn't exist in the string pool yet. Now, when we try to create iAmSameReference , jvm checks string pool and finds there is one object already created (iAmReference) and so will assign the reference of the same object to iAmSameReference.

if(iAmReference == iAmSameReference){
  System.out.println("References are always same"); 
}

Output: References are always same

Now, lets see what happens in case of new operator.

    static String iAmNewObject1 = new String("i am reference");
    static String iAmNewObject2 = new String("i am reference");

Here, both objects are created in the heap memory, no different than any other objects. To verify what we just discussed, try to execute below statements.

//compare reference with object.
if(!(iAmReference == iAmNewObject1)){
   System.out.println("Objects and reference are never same"); 
}
//compare objects created in heap memory.
if(!(iAmNewObject1 == iAmNewObject2)){
   System.out.println("Two objects are never same in java. Remember hash code ??"); 
}

Output: 
Objects and reference are never same.
Two objects are never same in java. Remember hash code ??

Now, what if you want both objects, iAmNewObject1 and iAmNewObject2, to put in string pool ?
Java provides method called intern() which provides this facility. So now let's put both of these objects in the string pool to save memory and avoid duplication of objects.

iAmNewObject1 = iAmNewObject1.intern();
iAmNewObject2 = iAmNewObject2.intern();

You can verify if objects are now created in the string pool or not by executing below statements:

if(iAmReference == iAmNewObject1){
   System.out.println("Now iAmNewObject1 is put on String pool from heap memory."); 
}

if(iAmNewObject1 == iAmNewObject2){
   System.out.println("Now iAmNewObject2 is also put on String pool from heap memory."); 
}
Output: 
Now iAmNewObject1 is put on String pool from heap memory.
Now iAmNewObject2 is also put on String pool from heap memory.

Hope, this will help you clear your doubts about java string handling.

Working Code can be found here.


Wednesday, October 8, 2014

Java Thread I: Thread Exception Handling with Example

This post will explain simple example of exception handling in java threads.

Have you ever wondered what happens when multiple threads are accessing shared data and one of them throws exception and you don't even know which thread did it and what to do next !!

What happens if one of the Strings is null and we get a NullPointerException? What happens if we run out of memory and get an OutOfMemoryError? What happens if we get an OOME and at the same time the connection does not work anymore? Then the finally would cause an exception, which would mask the OOME, and make it disappear. There are lots of possibilities, and if we try to cater for all eventualities (excuse the pun) then we will go crazy trying and our code will look rather complicated.

See below piece of code and try to see what could be the output here,

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

@Override
public void run(){
System.out.println("Creating thread");
int i = 1/0;
}

Output:
Creating thread
Creating thread
Exception in thread "Thread-1" java.lang.ArithmeticException: / by zero
at com.practice.java.threads.ThreadExceptionHandling.run(ThreadExceptionHandling.java:36)
at java.lang.Thread.run(Thread.java:619)
Exception in thread "Thread-0" java.lang.ArithmeticException: / by zero
at com.practice.java.threads.ThreadExceptionHandling.run(ThreadExceptionHandling.java:36)
at java.lang.Thread.run(Thread.java:619)

So in the real world, how are exceptions handled? Frequently, exceptions are stubbed out and ignored, because the writer of the code did not know how to handle the error (and was going to go back and fix it, one day, but the project manager was breathing down his neck and the release had to go out that afternoon). This is bad, since you then do not know that something has gone awry. On the other hand, if the exception bubbles up the call stack, it may kill the thread, and you may never know that there was an exception.

I have witnessed production code do things like this (I kid you not):


    try {

      // do something
    } catch(Exception ex) {
      // log to some obscure log file, maybe
      return "";
    }
  
The effect was that the webpage showed empty strings as values when something went wrong with the code.

My approach to exceptions is to have a central mechanism that deals with any exceptions that I am not 100% sure of how to handle. Whenever something goes wrong, this central place is notified. However, what happens when you are using someone else's code and their threads die without warning?


Java came up with uncaught exception handlers (Thread.UncaughtExceptionHandler) from jdk 1.5 onwards.


According to the Java API documentation, when a thread is about to terminate due to an uncaught exception, the Java Virtual Machine will query the thread for its UncaughtExceptionHandler using Thread.getUncaughtExceptionHandler() and will invoke the handler's uncaughtException method, passing the thread and the exception as arguments. If a thread has not had its UncaughtExceptionHandler explicitly set, then its ThreadGroup object acts as its UncaughtExceptionHandler. If the ThreadGroup object has no special requirements for dealing with the exception, it can forward the invocation to the default uncaught exception handler.


Java allows you to install a default exception handler for threads, for just this very reason. You have three options in doing so:

  • You can set it for a specific Thread  (custom handler for thread t)
  • You can set it for a ThreadGroup (which means for all Threads in that group)
  • You can set it VM wide for all Threads regardless (default handler)
In our example, where the Exception occurs in code we cannot change, we'll need to go with option 3. Doing so is very easy, and allows us to log the Exception that has occurred, get the full stack trace, and then restart the worker Thread since it will have terminated by the time the exception handler is notified. Here is the code modified to install a default exception handler (only the modified code is shown):

private class MyThreadExceptionHandler implements UncaughtExceptionHandler{

@Override

public void uncaughtException(Thread t, Throwable e) {
                   StringWriter sw = new StringWriter();
                   e.printStackTrace(new PrintWriter(sw));
                   String stacktrace = sw.toString();

System.out.println("ERROR! An exception occurred in " + t.getName() + ". Cause: " + e.getMessage());

                        System.out.println(stacktrace);
sendMail(stacktrace); //send mail to interested parties or stack holders.

}
}

Now, change the main() method as below:

public static void main(String[] args) {

Thread t1 = new Thread(new ThreadExceptionHandling());
Thread t2 = new Thread(new ThreadExceptionHandling());
Thread.UncaughtExceptionHandler exception = new ThreadExceptionHandling(). new MyThreadExceptionHandler();
      
// Add the handler to the thread object
Thread.setDefaultUncaughtExceptionHandler(exception);
// t1.setUncaughtExceptionHandler(custoException1);
// t2.setUncaughtExceptionHandler(customException2);
t1.start();
t2.start();
new RuntimeException(); //main class is also a thread and it doesnt have any exception handler.
}

Now, try to run the program and check the output. It makes life a lot easier since you don't have to break your head thinking of all possible failure scenarios upfront and make your code complex in handling them..................................

Working code for above example can be found here.