When program is executed, it will be loaded into memory.
And we cannot access the data directly in memory.
If you want to save the data to a file, we can use Serializable interface.
Sometimes, a class need execute some specific task when it is triggered by other class calling.
In other words, a morning call service is a callback mechanism in our life.
You tell waiter to call you at six every morning.
Then waiter will give you a morning call when time's up.
And we can use interface to achieve this concept.
publicinterfaceMorningCall{publicvoidcall();}
publicclassGuestimplementsMorningCall{publicstaticvoidmain(String[]args){WaitermWaiter=newWaiter();mWaiter.makeCall(this);};@Overridepublicvoidcall(){System.println.out("Guest: I Get Up.");}}
publicclassWaiter{publicvoidmakeCall(MorningCallmMorningCall){System.println.out("Waiter: Make a Morning Call At Six O'Clock.");mMorningCall.call();}}
Do we finish it? No. Let us think about multi-thread situation instead of single-thread.
Thread1: if (null == mSingleton) // This will be true.
Thread2: if (null == mSingleton) // This will be true.
Thread1: mSingleton = new Singleton();
Thread2: mSingleton = new Singleton();
Thread1: return mSingleton;
Thread2: return mSingleton;
Happened duplicate instances again! Let us added synchronized to protect.
Now, duplicate instances problem is solved. And the new problem is performance.
If there are many threads running, a thread need wait front thread until it finished work.
So we can do this. Initialed the Singleton class only once when program on started.
concat():
String has concat method, remember string is immutable.
It adds a string to another string.
It will create the new object after concatenation done, since it is a immutable.
append():
StringBuilder and StringBuffer has append method, remember these two are mutable.
It appends a char or char sequence to a string.
It will not create a new object, since it is a mutable one.
If you occurred below error message when you open a new Android Project, please upgrade JDK7 to JDK8.
Because Mac OS 10.10 Yosemite doesn't support JDK7 or lower.
Errors running builder 'Android Resource Manager' on Project java.lang.NullPointerException
General Notes:
Added support for Java 7 language features like multi-catch, try-with-resources, and the diamond operator. These features require version 19 or higher of the Build Tools. Try-with-resources requires minSdkVersion 19; the rest of the new language features require minSdkVersion 8 or higher. To use the new language features after installing ADT 22.6.0, ensure that you run Eclipse on JDK 7 and change your application project settings to use JDK 7.
The performance for ConcurrentHashMap is slightly worse when you need to use it from more than one thread but it is not a critical aspect of performance that you should be worried about.
A better approach would be to use a ThreadPool and a ScheduledExecutorService. Starting a thread from a thread is not inherently bad but may indicate a flaw in your design.
2.
In Java, all threads are owned by the process, and it doesn't matter from where they are started.
# cd /opt/jdk1.8.0_102/
# alternatives --install /usr/bin/java java /opt/jdk1.8.0_102/bin/java 2
# alternatives --config java
There are 1 programs which provide 'java'.
Selection Command
-----------------------------------------------
1 /opt/jdk1.8.0_101/bin/java
Enter to keep the current selection[+], or type selection number: 1
# alternatives --install /usr/bin/jar jar /opt/jdk1.8.0_102/bin/jar 2
# alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_102/bin/javac 2
# alternatives --set jar /opt/jdk1.8.0_102/bin/jar
# alternatives --set javac /opt/jdk1.8.0_102/bin/javac