Monday, December 30, 2013

Inter Thread Communication in Java using Wait Notify Example

Wait and notify methods in Java are used for inter-thread communication i.e. if one thread wants to tell something to another thread, it uses notify() and notifyAll() method of java.lang.Object. Classical example of wait and notify method is Producer Consumer design pattern, where One thread produce and put something on shared bucket, and then tell other thread that there is an item for your interest in shared object, consumer thread than pick than item and do his job, without wait() and notify(), consumer thread needs to be busy checking, even if there is no change in state of shared object.

Wednesday, December 25, 2013

How to redirect a page or URL using JavaScript and JQuery

Redirecting a web page means, taking user to new location. Many website use redirect for many different reasons, e.g. Some website redirect user from old domain to new domain, some redirect from one page to another e.g. a more relevant page. If you are Java programmer, and worked previously with Servlet and JSP, then you might be familiar with SendRedirect and Forward methods, which are used to redirect user in web applications. Actually there are two kinds of redirect, Server side redirect and client side redirect. In Server side redirect, server initiate redirection, while in client side redirect, client code does the redirection. Redirecting a web page using JavaScript and JQuery is a client side redirection.

Thursday, December 19, 2013

JUnit Testing Tips - Constructor is Called Before Executing Test Methods

Most of Java programmers either use JUnit or TestNG for there unit testing need, along with some mock object generation libraries e.g. Mockito, but not everyone spend time and effort to learn subtle details of these testing libraries, at least not in proportion of any popular framework e.g. Spring or Hibernate. In this blog post, I am sharing one of such details, which has puzzled me couple of years ago. At that time, though I had been using JUnit for significant time, I wasn't aware that code written inside constructor of Test class is executed before each test method.  This behaviour of JUnit has caused, some of my test to failed and putting hours of investigation in my code, without realizing that this is happening because of JUnit is initializing object by calling constructor before executing test method annotated with @Test annotation.

Thursday, December 12, 2013

How to configure Log4j in Java program without XML or Properties File

Sometime configuring Log4j using XML or properties file looks annoying, especially if your program not able to find them because of some classpath issues, wouldn't it be nice if you can configure and use Log4j without using any configuration file e.g. XML or properties. Well, Log4j people has thought about it and they provide a BasicConfigurator class to configure log4j programmatically, thought this is not as rich as there XML and properties file version is, but it's really handy for quickly incorporating Log4j in your Java program. One of the reason programmer prefer to use System.out.println() over Log4j, of-course for testing purpose, because it doesn't require any configuration, you can just use it, without bothering about XML or properties file configuration, but most programmer will agree that, they would prefer to use Log4j over println statements, even for test programs if it's easy to set them up.

Wednesday, December 4, 2013

When to make a method final in Java

Final keyword in Java is not as mysterious as volatile or transient, but still, it creates a lot of doubts on programmers mind. I often receive questions like, When to make a method final in Java or When to make a method static in Java, later I answered in my earlier post. Questions like this are not trivial, knowing what a keyword does is just small part of mastering that functionality. Similar to the real world, where knowing that a sword can cut a lot of things is not enough for a warrior to survive, you need to know how to use that and more importantly, use it effectively. Final keyword can be applied to a class, methods and variable and has a different meaning for each of them, but the motive remains same, it state completeness, it opposes change. For example, final class can not be extended, value of final variable cannot be changed and a final method can not be overridden in Java.