public enum UserActivity {
INSTANCE;
private String name;
private UserActivity() {
this.name = "xxyy";
}
public void dostuff() {
...
}
}
// use it as ...
UserActivity.INSTANCE.doStuff();
Book : Java Concurrency in Practice http://jcip.net/
code : https://github.com/manmustbecool/MyWiki/tree/gh-pages/myWikiCode/src
extends Thread
implements Runnable
implements Callable
The value of this variable will never be cached thread-locally: all reads and writes will go straight to “main memory”;
All classes have get and set methods that work like reads and writes on volatile variables.
private AtomicInteger c = new AtomicInteger(0);
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.
only a single thread can execute a block of code at the same time
synchronized void dostuff(){
...
}
or
void dostuff(){
...
synchronized(this){...}
}
Complexities: average O(log(n)) worst O(log(n)) best O(1)
input sorted array
code : https://github.com/manmustbecool/MyWiki/tree/gh-pages/myWikiCode/src
code : https://github.com/manmustbecool/MyWiki/tree/gh-pages/myWikiCode/src
code : https://github.com/manmustbecool/MyWiki/tree/gh-pages/myWikiCode/src