Monthly Archives: December 2017

default keyword in Java

  1. default can be used in switch statement.
  2. default can be used in annotation, specifying the default value. For example:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Todo {
    String author() default "Yash";
}

@Todo(author = "Yashwant")
public void incompleteMethod1() {
    //Some business logic is written
    //But it’s not complete yet
}

Local/Instance/Class variable

Local variable, variable defined inside method, constructor or block.
Instance variable, a variable inside class. It can be instantiated.
Class variable, inside class, but outside method. Must be static. Only one copy per class.

public class MyTest {

    static String str = "abcd";  // classs variable, only one copy

    private String a = "123";   // instance variable, can be copied by instantiation

    public int calculate() {
        int a = 1;  // local variable
        return a;
    }

}

Vector Clock better than Lamport Clock

Lamport clock. Algorithms: ts = max(local stamp, msg timestamp) + 1.
Problem, “So Lamport Clocks are great for making sure we know when there’s a causal dependency between records. But there is a problem. Because Lamport Clocks induce a total ordering over all records, they actually imply more dependencies than truly exist. If two records are not causally related at all, and were produced completely independently by separate nodes that did not communicate, they will still have Lamport Clock timestamps which imply that one is ordered before the other, which is a false positive.”
youtube

Vector Clock. Answer the order of two events, if two events are concurrent.
youtube

Example that Vector Clock better than Lamport Clock
vector_lamport

synchronized and synchronized static

Below are 2 code snippets. doSomething() in first one is synchronized. doSomething() in second one is synchronized static.
For synchronized:
When calling doSomething() within 2 different instances at the same time, it can be concurrent.
When calling doSomething() within same instance at the same tim, it can’t be concurrent.

For synchronized static:
When calling doSomething() within any instances at the same time, it can’t be concurrent.

public class SynchronizedDefaultClass {

    public static int a;

    // with synchronized, this method could be called only by 1 thread.
    public synchronized void doSomething() {
        for (int i = 0; i < 5; i++) {
            a++;
            System.out.println(String.format("Thread: %s, value: %d", Thread.currentThread().getId(), a));
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
            }
        }
    }

}
public class SynchronizedStaticClass {

    public static int a;

    // with synchronized, this method could be called only by 1 thread.
    public synchronized static void doSomething() {
        for (int i = 0; i < 5; i++) {
            a++;
            System.out.println(String.format("Thread: %s, value: %d", Thread.currentThread().getId(), a));
            try {
                Thread.sleep(1000);
            } catch (Exception e) {}
        }
    }

}

Caller1:

public class Caller1 implements Runnable {

    SynchronizedDefaultClass synchronizedClass;

    public Caller1(SynchronizedDefaultClass synchronizedClass) {
        this.synchronizedClass = synchronizedClass;
    }

    public void run() {
        synchronizedClass.doSomething();
    }

    /*
    Call a synchronized method belonging to same instance.
     */
    public static void main(String[] args) {
        SynchronizedDefaultClass synchronizedClass = new SynchronizedDefaultClass();
        Thread c1 = new Thread(new Caller1(synchronizedClass));
        Thread c2 = new Thread(new Caller1(synchronizedClass));
        c1.start();
        c2.start();
    }

}

Caller2:

public class Caller2 implements Runnable {

    SynchronizedDefaultClass synchronizedClass;

    public Caller2(SynchronizedDefaultClass synchronizedClass) {
        this.synchronizedClass = synchronizedClass;
    }

    public void run() {
        synchronizedClass.doSomething();
    }

    /*
    Call a synchronized method belonging to different instances.
    */
    public static void main(String[] args) {
        SynchronizedDefaultClass synchronizedClass1 = new SynchronizedDefaultClass();
        SynchronizedDefaultClass synchronizedClass2 = new SynchronizedDefaultClass();
        Thread c1 = new Thread(new Caller2(synchronizedClass1));
        Thread c2 = new Thread(new Caller2(synchronizedClass2));
        c1.start();
        c2.start();
    }

}

Caller3:

public class Caller3 implements Runnable {

    SynchronizedStaticClass synchronizedClass;

    public Caller3(SynchronizedStaticClass synchronizedClass) {
        this.synchronizedClass = synchronizedClass;
    }

    public void run() {
        synchronizedClass.doSomething();
    }

    /*
    Call a synchronized static method belonging to different instances.
    */
    public static void main(String[] args) {
        SynchronizedStaticClass synchronizedClass1 = new SynchronizedStaticClass();
        SynchronizedStaticClass synchronizedClass2 = new SynchronizedStaticClass();
        Thread c1 = new Thread(new Caller3(synchronizedClass1));
        Thread c2 = new Thread(new Caller3(synchronizedClass2));
        c1.start();
        c2.start();
    }

}

github