1 Singleton using enum

public enum UserActivity {
    INSTANCE;

    private String name;

    private UserActivity() {
        this.name = "xxyy";
    }

    public void dostuff() {
     ...
    }
}

// use it as ...
UserActivity.INSTANCE.doStuff();

2 Concurrency

Book : Java Concurrency in Practice http://jcip.net/

2.1 Thread

code : https://github.com/manmustbecool/MyWiki/tree/gh-pages/myWikiCode/src

  • extends Thread

  • implements Runnable

  • implements Callable

    • Similar to Runnable, but a Runnable does not return a result and cannot throw a checked exception.

2.2 Variables

2.2.1 Volatile

The value of this variable will never be cached thread-locally: all reads and writes will go straight to “main memory”;

2.2.2 Atomic Variables

All classes have get and set methods that work like reads and writes on volatile variables.

private AtomicInteger c = new AtomicInteger(0);

2.2.3 ConcurrentHashMap

Hashtable is similar to Collections.synchronizedMap(Map) - only one thread can access the map at the same time.

ConcurrentHashMap concurrent modification of the Map from several threads without the need to block them.

2.3 Synchronized

only a single thread can execute a block of code at the same time

synchronized void dostuff(){
  ...
}

or

void dostuff(){
  ...
  synchronized(this){...}
}

4 Sort

http://bigocheatsheet.com/

4.1 Quick Sort

  1. Select a pivot, normally the middle one
  2. From both ends, swap elements and make all elements on the left less than the pivot and all elements on the right greater than the pivot
  3. Recursively sort left part and right part

code : https://github.com/manmustbecool/MyWiki/tree/gh-pages/myWikiCode/src

4.2 Bubble Sort

  1. Compare each pair of adjacent elements from the beginning of an array and, if they are in reversed order, swap them.
  2. If at least one swap has been done, repeat step 1.

code : https://github.com/manmustbecool/MyWiki/tree/gh-pages/myWikiCode/src

5 java 8

http://winterbe.com/posts/2014/03/16/java-8-tutorial/

^Home Page^