Posts

Showing posts from May, 2012

Atomicity of Reference assignment - Java

     Java's JVM specifications ( http://docs.oracle.com/javase/specs/jls/se7/jls7.pdf ) claims that writes to and reads of references is atomic on both 32 bit and 64 bit implementation and i happened to have a code which enforced mutual exclusion on the destination variable, public void set(Test ref) {   synchronized (this)   {      // obj is the member variable      obj = ref;   } }    At some point the need for synchronization was in question, as write into a reference is atomic and i started looking at underlying assembly instructions for the above code without synchronized block. I used 64 bit implementation of Open JDK 7 Update 3 on Ubuntu running on Intel x86 64 bit processor. The assembly code turned out as, Decoding compiled method 0x00007f8c5d061110: Code: [Entry Point] [Constants] # {method} 'set' '(LTest;)V' in 'Test' # this: rsi:rsi = 'Test' # parm0: rdx:rdx = 'Test' ...

Java : Package specific static initialization

Image
       I was working on a sample android application having an activity and a service, declared in the same package. The application logic didn't enforce a specific order of creation and had to perform static initialization irrespective of whichever application component was created first by android framework (which ever class was loaded by the DEX class loader of dalvik virtual machine). This basically boils down to a Java package level static initialization and i had to settle for a workaround using singleton pattern Init enforces a singleton pattern and exposes a static function (initialize) invoked by the static blocks of both Activity and Service.

Android - Find process id of service

     Ever felt the need to find the id of the process hosting a service via code and thought of extracting the same output of "adb shell ps | grep "?          It seems to work except for the fact that searching by process name is not reliable. However, services in android world are uniquely identified by their name (string) and if you have access to android platform source code, just pull in this change,           https://android-review.googlesource.com/#/c/19269/      And its just a question to invoking a API           ServiceManager . getServicePid ( "media.audio_flinger" ); Update:  Few folks have reached out for an existing solution as the Patch was never merged. The closest alternative i came across is to get the pid based on the command line which was used to launch the service process. This works for services hosted in its own process space like m...