2016年10月11日 星期二

Design Pattern and Idiom

C++
NVI Idiom (Non-virtual interface, by creating a public non-virtual function that calls virtual private function that can be overridden by child class)

Java
"Double-Checked Locking" idiom, by checking null once, locking if null, and double checking null again, then initialize. It seems correct but it just works sometimes because by chance it is possible that thread B access the variable while thread A is locking and is partially creating the object. The correct solution is that after Java 5 the field should be declared volatile. The Java volatile field is equivalent to C++ atomic data member, allowing atomic assignment, and hence when thread B sees the variable it must be atomically fully created.

沒有留言:

張貼留言

2025 My Hardware Personal Computer Used

 2025 My Hardware Personal Computer Used Check Ubunut sudo dmidecode -t 2 sudo lsblk -o NAME,FSTYPE,LABEL,MOUNTPOINT,SIZE,MODEL 2025 Intel(R...