Visual studio debug

I was trying to debug some ms office add in, then noticed this cool featurr of VS 2010.

Instead of starting the project in debug mode, which someitmes could have various issues, just start the ms office, then through VS debug –> attach process, to link the project source code is quick and effective option.

 

 

Unbrick LG G3

If you are a LG G3 user, you might be tempted to update to marshmallow, seeing the updates available through LG PC suite. Please don’t !

If it happened you already did, you might now end up in a situation like a lot other LG G3 users, where the phone would bricked at bootloop. Basically, the phone would infinitely restart. However, it never restart completely, where it just keep relaunching till the boot screen, then off and reboot till the boot screen again.

If you are already in above situation, my experience here might help. I have encountered this during the weekend, and almost gave up to customer service centre for last resolt. Then somehow below trial worked !

(Leave the power button aside, you never need to touch that button during this whole procedure.)

  1. power off the phone by removing the battery
  2. with the phone still off, plug the usb, connect the phone with PC
  3. with LG PC suit opened on the PC, and the phone still off, just hold the Power UP button for few seconds, this move the phone into download mode
  4. At LG PC Suite, Under “Mobile Devide” menu, there is this option called “Restore upgrade errors”. click it, it will try to connect the phone, download the patch to revert previous update. wait till it finish, your phone would be auto restarted.

In case above steps didn’t work out on first try, try another one or twice. It could have patched multiple times, if you phone is moving from lower lollipop version.

Let me know if it helps !

Raspberry PI as NAS

Just finished setting up my RP 2 as a NAS. Here is the summary list to achive that:

Hardware
1. Raspberry PI 2
2. USB hard disk
3. HDMI cable
4. TV
5. USB keyboard and Mouse
6. Internet cable
7. SD Card

OS
I have tried Raspberry PI official raspbian, which turns out extremely laggy. Then switched to Ubuntu 14 LTS, which doesn’t work well on RP 2 directly.
Finally installed ubunut mate, which works very smooth and almost perfect.

One thing to note to install ubuntu mate on RP 2, format the SD card to ext4 format would be the easiest approach.

NAS
to have a full functional NAS, i have installed and set up using below:
1. miniDLNA
pretty light and straight forward set up, just include the folder you would like to DLNA, and have the app auto start
2. USB hard disk normally have issue with direct power up from Rp 2, due to the lmited power from the usb port. a lot suggested to use usb hub, while for myself, a dual usb connector worked very well
3. mount the hard disk, for my case, is mounted to /media
4. there are several vnc servers, i have used vnc4server, pretty light and worked perfectly
5. enable SSH
with above two steps, keyboard, mouse and Display for RP is seldom in necessary
6. set up samba
add the shared directory, on /etc/samba/smb.conf
7. auto download
i have set up aria2c, but seldom used. as there is a chinese software, Thunder remote works pretty nice

PS. from your home computer, if you have xming installed, which could mostly replace the need for vncviewer as well

JIT hotspot compiler

The Java JIT compiler compiles Java bytecode to native executable code during the runtime of your program. This increases the runtime of your program significantly. The JIT compiler uses runtime information to identify part in your application which are runtime intensive. These so-called “hot spots” are then translated native code. This is the reason why the JIT compiler is also called “Hot-spot” compiler.

JIT is store the original bytecode and the native code in memory because JIT can also decide that a certain compilation steps must be revised.

Java volatile again

http://en.wikipedia.org/wiki/Volatile_variable

The Java programming language also has the volatile keyword, but it is used for a somewhat different purpose. When applied to a field, the Java volatile guarantees that:

    (In all versions of Java) There is a global ordering on the reads and writes to a volatile variable. This implies that every thread accessing a volatile field will read its current value before continuing, instead of (potentially) using a cached value. (However, there is no guarantee about the relative ordering of volatile reads and writes with regular reads and writes, meaning that it's generally not a useful threading construct.)
    (In Java 5 or later) Volatile reads and writes establish a happens-before relationship, much like acquiring and releasing a mutex.[9]

Using volatile may be faster than a lock, but it will not work in some situations.[citation needed] The range of situations in which volatile is effective was expanded in Java 5; in particular, double-checked locking now works correctly.[10]

http://en.wikipedia.org/wiki/Happened-before

In computer science, the happened-before relation (denoted: \to \;) is a relation between the result of two events, such that if one event should happen before another event, the result must reflect that.

Fail fast vs fail safe

fail fast, throw exception if any concurrent modification.

fail safe, work on seperate/clean copy for modification, dont throw exception.

 java.util.concurrent.CopyOnWriteArrayList<E>

A thread-safe variant of java.util.ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array. 

This is ordinarily too costly, but may be more efficient than alternatives when traversal operations vastly outnumber mutations, and is useful when you cannot or don't want to synchronize traversals, yet need to preclude interference among concurrent threads. The "snapshot" style iterator method uses a reference to the state of the array at the point that the iterator was created. This array never changes during the lifetime of the iterator, so interference is impossible and the iterator is guaranteed not to throw ConcurrentModificationException. The iterator will not reflect additions, removals, or changes to the list since the iterator was created. Element-changing operations on iterators themselves (remove, set, and add) are not supported. These methods throw UnsupportedOperationException. 

All elements are permitted, including null. 

Memory consistency effects: As with other concurrent collections, actions in a thread prior to placing an object into a CopyOnWriteArrayList happen-before actions subsequent to the access or removal of that element from the CopyOnWriteArrayList in another thread. 

This class is a member of the Java Collections Framework.

Fail-Safe vs Fail-Fast Iterator in Java Collections
Fail-Safe Iterator (java.util.concurrent – ConcurrentSkipListSet, CopyOnWriteArrayList, ConcurrentMap)

Fail-safe Iterator is “Weakly Consistent” and does not throw any exception if collection is modified structurally during the iteration. Such Iterator may work on clone of collection instead of original collection – such as in CopyOnWriteArrayList. While ConcurrentHashMap’s iterator returns the state of the hashtable at some point at or since the creation of iterator. Most collections under java.util.concurrent offer fail-safe Iterators to its users and that’s by Design. Fail safe collections should be preferred while writing multi-threaded applications to avoid conurrency related issues. Fail Safe Iterator is guaranteed to list elements as they existed upon construction of Iterator, and may reflect any modifications subsequent to construction (without guarantee).

Fail-Fast Iterator (java.util package – HashMap, HashSet, TreeSet, etc)

Iterator fails as soon as it realizes that the structure of the underlying data structure has been modified since the iteration has begun. Structural changes means adding, removing any element from the collection, merely updating some value in the data structure does not count for the structural modifications. It is implemented by keeping a modification count and if iterating thread realizes the changes in modification count, it throws ConcurrentModificationException. Most collections in package java.util are fail-fast by Design.