algorithms (22) Design Patterns (20) java (19) linux (14) Snippet (13) service mix (6) soa (4)

Sample program in java for Deadlock

package concurrency;
public class DeadLock {
    public static void main(String[] args) {
        JavaFile firstFile = new JavaFile("file1");
        JavaFile secondFile = new JavaFile("file2");
        JavaCoder firstCode = new JavaCoder(firstFile, secondFile, "first coder:");
        JavaCoder secondCoder = new JavaCoder(secondFile, firstFile, "second coder:");
class JavaCoder extends Thread {
    public void run() {
        System.out.println(Thread.currentThread().getName() + "trying to checkin " + firstfile);
    private JavaFile secondfile;
    private JavaFile firstfile;
    JavaCoder(JavaFile firstFile, JavaFile secondFile, String name) {
        this.firstfile = firstFile;
        this.secondfile = secondFile;
class JavaFile {
    private String name;
    JavaFile(String name) { = name;
    public String toString() {
    public synchronized void checkIn(JavaFile otherFile) {
        System.out.println(Thread.currentThread().getName() + "in check-in " + this);
        System.out.println(Thread.currentThread().getName() + "completing check-in " + this);
    public synchronized void checkOut() {
        System.out.println(Thread.currentThread().getName() + "in check-out " + this);
        while (true) {
Sample Output
first coder:trying to checkin file1
first coder:in check-in file1
first coder:in check-out file2
second coder:trying to checkin file2 ( This is the dead lock , second coder is trying to checkin file 2 which is locked by first coder for check-out)

Stop a thread in java

static boolean stopRequested = false;
    public static synchronized boolean isStopped() {
        return stopRequested;
    public static synchronized void stopRequested() {
        StopThread.stopRequested = true;
    public static void main(String ar[]) throws InterruptedException {
        Thread runningThread = new Thread(new Runnable() {
            public void run() {
                while (!isStopped()) {
        System.out.println("Stop Running Thread");

Effective JUNIT's

PIT best place to start
Mutations supported and The details of each type of mutation are here
not all are enabled by default (please check I am not sure)

How to use with Maven
1)   Add the plugin

2)   Execute the command mvn org.pitest:pitest-maven:mutationCoverage
3)   Check the report in \target\pit-reports

Sonar has a plugin  for pit tests which adds these survived mutations as violations

Eclipse :
To install pit for eclipse refer

Java Object Class

it is the base class of all the classes in java

 Methods in Java Object class

  • hashCode:
    • the more unique the hashCode is the more efficient is the hash map/table
    • used to find the bucket in which an object goes into in a hash table.
    • more than one invocation of hashCode of the same object should return same Integer.
    • if two objects are equal according to equals method then their hashCode must be same.
  • equals:
    • is used in hashMap when two objects have same hashCode.
    • if two objects are equal their hashCode must be same.
    • it should be
      • reflexive : x.equals(x)
      • symmetric:  x.equals(y) and y.equals(x)
      • transtive: x.equals(y) y.equals(z)   implies => x.equals(z) 
      • consitent: x.equals(y)  should allways return same value.
      • for null it should be false: x.equals(null)  false
    • the default behaviour of equals = “==” i.e is object comparision
  • toString :
    • the default implementation is
    • classname  @ hex format of the hashCode.
    • its good to override toString .
    • also should be concise and informative.
  • clone:
    • to clone the object .
    • x.clone() != x
    • x.clone().getClass() == x.getClass() Not mandatory *
    • x.clone().equals(x) Not mandatory *
    • alternative to clone is copy constructor

Search 24 Bytes