Tuesday, August 20, 2013

My personal Avaniavittam reminders

Avaniavittam is a festival day when we change the sacred thread (poonal).

This time, I had missed a few things and had to get ready quickly.  I thought I would keep a Things to do for my own reference next year.

Week before

  1. Make sure you have
  2. Poonal
  3. Dharbai
  4. Powthiram
  5. Black Sesame
  6. Rice
  7. Sandhyavandhanam book
  8. All mantra details - like greeshma rithu, varsha rithu etc.



Day before

  1. Materials to make vadai and payasam
  2. Vaccum clean the place
  3. Saree for wife
  4. Silk veshti
  5. Alarm



Tuesday, August 13, 2013

The Set

A common pattern in an interview is to ask for a data structure that would store a collection of items.  In Java, there are three types of objects that holds collection of data - Map, Set and List.

Here is a tip that would make sure that you choose an appropriate data type: Set should be your default choice.  A choice for a List should be based on you reasoning yourself out of using a Set.

Why?
Set has this data structure called HashSet.  It is super efficient.  Let us see this table for CURD operations.


HashSetArrayListLinkedList
C - Add to Collection
1
n
1
R - Find in Collection
1
lg n if sorted
n otherwise
n
D - Remove from Collection
1
n remove and move all elements
1 (finding will take n. but removing will be 1)

As you can see, HashSet is super-efficient in all cases.

When should I not use Set?

1. If your collection can allow duplicates
2. If you need to maintain an ordering of some kind.

Always use a Set by default.  Set is wonderful.

Android App not getting published

I created an Android App.  I followed all steps given here

I waited for almost two days.  The App did not get published.  It usually takes 5-24 hrs to get published.

Then I figured out that I had uploaded APKs under Alpha, Beta and Production.  But the App had remained in Alpha.  So, I moved the application to Production.

Here is the screenshot of the page:

Developer Console -> Your App -> APK -> Production tab -> Move to (drop-down)-> Choose Production.

Thursday, August 1, 2013

Android: Wake Up - Mistakes that I made

I had been working on a very simple Android Alarm App.  The users would set up an alarm after certain period of time.  When the time for Alarm arrives, the BroadcastReceiver defined in our application receives a signal.  If the phone is in lock mode, we have to wake the phone up and display to the user that it is time.

To wake the phone up, I am using android.os.PowerManager and PowerManager.WakeLock.

  1. I had tried waking the phone up from inside BroadcastReceiver.  It should be done from Activity's onCreate() method.
  2. The parameters that I passed for getting a new WakeLock were not working right.  In fact, I don't know which is the correct parameter I should use.  I am now using a deprecated parameter which is the only thing that is working for me.  The method is PowerManager'sInstance.newWakeLock().  The first parameter is levelAndFlags - I am using PowerManager.SCREEN_DIM_WAKE_LOCK.  I know I will have to use a combination of different non-deprecated flags.  But, I have not figured it out yet.
  3. Always release the WakeLock in onFinish(), onPause(), onStop() methods.


Thursday, July 11, 2013

HashSet delegates all its functions to HashMap



This is an old one.  In Java, HashSet delegates all its functions to HashMap.

What goes on under the covers of a HashSet<T> is:

  • A member variable, map, of type HashMap<T, Object> is maintained.
  • All operations on HashSet is delegated to a corresponding operation on HashMap - add(), remove(), contains(), etc.
  • A dummy object



add(T objOfTypeT)
Invokes put(objOfTypeT, dummy object)

remove(T objOfTypeT)
Invokes HashMap's remove(objOfTypeT)

size()
returns map's size

contains(T objOfTypeT)
returns map.containsKey(objOfTypeT)

iterator()
returns map's keySet's iterator

Thursday, July 4, 2013

Installing packages from Python

I am new to python.  I am using python 2.7.5.

I was working on writing a screen scrapper for one of my projects.  I am using scrapy framework for screen scrapping.

After setting up and testing my spider, I wanted to test how it could be called from another Python script.  Till this point I had been testing using scrapy's commands.

I looked up online to find an example to call scrapy from another Python script.  Scrapy, it seems, uses a network framework called twisted.  Twisted was not installed in my machine.  So, I proceeded to do a sudo apt-get install python-twisted in my command prompt.  That did not seem to work.

I created a sample twisted script and attempted to run that.  This would help in testing if Python really recognizes Twisted  Obviously, that failed.

So, after a bit of looking around, I stumbled upon this.  Python packages needs to be installed from tar files.  So, download a tar file, un-tar it.  This should create a source directory which is supposed to have a file called setup.py.  To install the package, run python setup.py install 

This is exactly what I did.  I downloaded tar of Twisted from here.  Then un-tar'ed it using tar xjf Twisted-13.1.0.tar.bz2 which created a folder Twisted-13.1.0.  Then changed directory into Twisted-13.1.0 using cd Twisted-13.1.0.  Finally, python setup.py install.  This gave an error.  Twisted required another package called zope.interface.

I downloaded tar of zope.interface from here and then did the above steps first for zop.interface so zope gets installed.  Then proceeded to Twisted-13.1.0 folder to do a python setup.py install in that folder.

So, in general, if you need to install a package in Python, get the tar file and use python install feature on setup.py file of the tar.

Monday, June 24, 2013

To synchronize or not to synchronize?

Let us say we implement a very common functionality and offer our implementation as a open source library.  For example, we develop a library to keeping track of count of how many times a certain operation has taken place - like a call to web service.  We know this is very reusable.  But, we do not know how this functionality will be used by developers.  There could be many concerns on how this library could be used by the developers; but in this post, let us concentrate on: will the functionality be called from a multi-threaded environment?

The traditional way of handling multi-threading is to ensure make a critical-section is accessed in a single-threaded way.  To achieve this in Java, we use synchronized key word.  synchronized gets a lock (atomically) on the object (non-static methods) or class (static methods).  In Java, we also have an option of maintaining lock on an arbitrary object.  Code that is safe for execution by multiple threads is called thread-safe.

Now, the downside of synchronized is that it takes a lot of performance-hit.  For a fast program, it is advisable to use synchronized in as few places as possible - (a) this will reduce the performance-hit produced by synchronized (b) it ensures that as many threads executes in parallel as possible - both of which are good for performance.

So, given these facts about synchronized, in our Counter library, should we synchronize to make it thread-safe or not synchronize to make it fast?

The advisable approach is: do not synchronize unless you are sure that the code will be used in multi-threaded environment.

So, we would not make our Counter's methods synchronized.  If a developer intends to use our Counter class in a multi-threaded code, then the developer should ensure that the methods are called in a thread-safe manner.  This can be done by synchronizing over Counter object (or some other lock object) or by providing a wrapper method that is synchronized on the calling object.

Some simple multi-threading tips:

  • Always check if object is used in a multi-threaded environment.
  • Make the object immutable.  This means that you set values to an object's state only in constructor.  There should be no method accessible to other classes that can change the state of object after constructor has finished execution.
  • If you are dealing with a group of objects, use a separate lock object and synchronize based on that lock object.