Wednesday, July 1, 2015

How to use wait, notify and notifyAll in Java - Producer Consumer Example

You can use wait, notify and notifyAll methods to communicate between threads in Java. For example, if you have two threads running in your program e.g.Producer and Consumer then producer thread can communicate to consumer that it can start consuming now because there are items to consume in queue. Similarly a consumer thread can tell producer that it can also start putting items now because there is some space in queue, which is created as a result of consumption. A thread can use wait() method to pause and do nothing depending upon some condition. For example, in producer consumer problem, producer thread should wait if queue is full and consumer thread should wait if queue is empty. If some thread is waiting for some condition to become true, you can use notify and notifyAll methods to inform them that condition is now changed and they can wake up. Both notify() and notifyAll() method sends notification but notify sends notification to only one of the waiting thread, no guarantee which thread will receive notification and notifyAll() sends notification to all threads. So if only one thread is waiting on an object lock, also known as monitor then both notify and notifyAll wil send notification to it. If multiple threads are waiting on a monitor then notify will only inform one of the lucky thread and rest will not receive any notification, but notifyAll will inform all threads. In this Java multi-threading tutorial you will learn how to use wait, notify and notifyAll() method in Java to implement inter thread communication by solving producer consumer problem. BTW, if you are serious about mastering concurrency and multi-threading, I strongly suggest you to read Java Concurrency in Practice by Brian Goetz, without reading that book your journey to Java multi-threading is not complete. Its probably one of the most recommended book to Java developers.

Tuesday, June 30, 2015

Difference between Dependency Injection and Factory Pattern in Java

TL;DR Main difference between dependency injection and factory pattern is that in case of former dependency is provided by third party (framework or container) while in case of later dependency is acquired by client class itself. Another key difference between them is that use of dependency injection result in loosely coupled design but use of factory pattern create tight coupling between factory and classes which are dependent on product created by factory. Though both Dependency Injection and Factory pattern looks similar in a sense that both creates instance of a class, and also promotes interface driven programming rather than hard coding implementation class; But, there are some subtle differences between Factory pattern and dependency injection pattern. In case of factory design pattern, client class is responsible for calling getInstance() of factory class to create instance of products, it also means that client class is directly coupled with factory and can't be unit tested without factory class being available. On the other hand in Dependency Injection, client class has no clue about how his dependencies are created and managed. It only knows about dependencies. Mostly dependencies are injected by framework e.g. bean class exists without any hard coded dependency, as those are injected by IOC container e.g. Spring. You can also used points used here to answer questions like difference between Spring IOC and Factory pattern because Spring IOC is nothing but an implementation of dependency injection pattern. BTW, if you are serious about learning design patterns and principles, I suggest you to take a look at Head First Object Oriented Analysis and design book. This book is overshadowed by its popular cousin Head First Design Pattern but its one of the book to master object oriented design principles and patterns.

Saturday, June 27, 2015

2 Ways to check If String is Palindrome in Java? Recursion and Loop

A String is said to be Palindrome if it is equal to itself in reverse order. You can use this logic to check if String is Palindrome or not. There are two common ways to find if a given String is Palindrome or not in Java, first by using for loop, also known as iterative algorithm and second by using recursion, also known as recursive algorithm. The crux of this problem lies in how do you reverse String in Java? because once you have the String in reverse order, problem reduced to just comparing itself with the reversed String. If both are equal then given String is Palindrome otherwise it's not. Also whether your solution is iterative or recursive will also determine by implementing this logic. If you reverse String using for loop then it become an iterative solution and if you reverse String using recursion then it become a recursive solution. In general, recursive solution are short, readable and more intuitive but subject to StackOverFlowError and that's why not advised to be used in production system. You should always be using iterative solution in production, unless your programming language supports tail recursion optimization e.g. Scala which eliminates risk of StackOverFlowError by internally converting a recursive solution to an iterative one. If you are doing this exercise as part of your Interview preparation then I suggest you to take a look at Cracking the Coding Interview: 150 Programming Questions and Solutions, as title says it contains 150 good questions based upon different topics e.g. String, array, linked list, binary tree, networking etc. A good book for preparing both Java and C++ interview.