Tuesday, December 31, 2013

How to download source of dependencies jars using Maven

To download source of dependencies jars in local repository while building project using Maven, you need to add "dependency:sources" goal while building your project.

Run following command for your project and it will download all sources for dependencies if it exists.
Command : mvn dependency:sources

You can find more information related to dependency plugin at Maven Dependency Plugin project.


Sunday, December 1, 2013

Java: List of books you must read

Here is list of must read books if you are java developer.
1. Head first OOAD
2. Effective java
3. Java puzzlers
4. Java concurrency in practice
5. Design patterns by GOF
6. Spring in action
Your comments are welcome if you want to add to this list.

Monday, August 26, 2013

Java: Thread program to demonstrate wait and notify

Below question was asked to one of my friend during interview:

Write program to create 2 Threads "Jack" and "Jill". You write a program which will print " Jack Jill Jack Jill"
Thread execution:  Thread1, Thread2, again Thread2, then Thread2.


I tried to write program for above problem :
T1. Java:


public class T1 implements Runnable {
private static int count = 0;
public synchronized void run() {
int threadCount = 0;
while (true) {
count++;
threadCount = count;
System.out.println(Thread.currentThread().getName());

try {

notifyAll();
while (threadCount >= count) {
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}

// If you comment below if block, Jack , jill threads will run
// forever one after another.
// So added to check if Jack and Jill threads run one after another
// 2 times
if (count >= 4) {
count++;
System.out.println(Thread.currentThread().getName() + " End");
notifyAll();
break;
}
}

}
public static void main(String[] args) {
T1 t = new T1();
Thread t1 = new Thread(t, "Jack");
Thread t2 = new Thread(t , "Jill");
t1.start();
t2.start();
}

}


Output:

Jack
Jill
Jack
Jill
Jack End
Jill End