Saturday, February 11, 2012

It’s not hard to decide what you want your life to be about. What’s hard, she said, is...


“It’s not hard to decide what you want your life to be about. What’s hard, she said, is figuring out what you’re willing to give up in order to do the things you really care about.”

- “Bittersweet” by Shauna Niequist through Julie911

Maintaining character in every moment


My understanding:

Not all horses are skilled at being swift
Not all dogs are skilled at sniffing
Since I am naturally dull, shall I, for that reason, not take pains?
NO. One should continue to take pains.

I may not be the best body builder in the world, yet, I will not neglect my body.
I may not be the best rich man in the world, yet, I will not neglect my property

In short we do not neglect looking after anything because we despair(loose hope or complete absence of hope) of reaching the highest degree.

- A Selection From The Discourses of Epictetus With The Encheiridion

Thursday, February 02, 2012

Strong, Weak, Soft and Phantom references in Java

The following code instantiates an instance of Student class in the heap and references the created instance from the stack variable s.

Student s = new Student();

The instantiated Student object remains in the heap until it is reachable by at least one reference to it. When the garbage collector runs after the last object reference has been removed, the object is deleted from heap. 

In the above example, s is a strong reference to the Student object. The garbage collector will not remove objects that have a strong reference.

When strong references fall short
There are times when a object should be marked as garbage collectible even when there are active references to it. For example, consider a large image object held in a in-memory cache. This image object should remain in the heap as long as the caching API's clients have reference to the image object. But if the caching API uses strong references, the image object will never get garbage collected as there will always be a strong reference to the image object from the cache itself. Enter weak references.

In the above caching example, the caching API should use a WeakReference. A weak reference "is a reference that isn't strong enough to force the object to remain in memory".

WeakReference weakStudent = new WeakStudent(new Student());

weakStudent.get(); // Returns actual Student object

The weakStudent.get() call could potentially return null if there are no strong references to the Student object.

A WeakHashMap is similar to HashMap except that the keys (not values) are referred to using weak references. So when there are no other Strong references to a key, it will be removed from the map.

Once WeakReference.get() starts returning null, the reference has become garbage collectible and the WeakReference is of no good. The ReferenceQueue class helps keep track of such garbage collectible references and should be passed as argument to WeakReference's constructor. When a object becomes garbage collectable, it will be placed in the ReferenceQueue. The application can read this queue from time to time and perform clean up on its end.

Degrees of weakness
A WeakReference is only one flavor of weakness. There are 2 more flavors:
1. SoftReference - This is less weak than a WeakReference. In a WeakReference, the referenced object is garbage collected the next time garbage collector runs, irrespective of whether memory is in shortage or not. A SoftReference is garbage collected only when memory is in short supply.
2. PhantomReference - This is stronger than a WeakReference. Its get() method always returns null. Its only use is to figure out when the object is enqueued into ReferenceQueues. For a WeakReference, the object is queued as soon as its only weakly reachable. Even before finalization and actual garbage collection. For PhanthomReference, the object is queued only after the object is physically removed from memory.

Saturday, December 03, 2011

To be upset over what you don't have is to waste what you do have.

To be upset over what you don't have is to waste what you do have.

- Ken Keyes Jr.

The minute you settle for less than you deserve

The minute you settle for less than you deserve, you get even less than you settled for.
- Maureen dowd

Wednesday, November 30, 2011

Better to take many small steps

“It is better to take many small steps in the right direction than to make a great leap forward only to stumble backward.”

Louis Sachar through Julie911

Saturday, October 08, 2011

3 ways to initialize and destroy Spring beans

There are 3 ways to initialize (and destroy) spring beans
1. Using init-method and destroy-method attributes
2. Implementing InitializingBean and DisposableBean interfaces
3. Using @PostConstruct and @PreDestroy Annotations (available only in Spring >=2.5)


1. Using init-method and destroy-method attributes
Certain bean methods can designated as initializing and destroying methods using the init-method and destroy-method attributes. Like so:



<bean
    id="studentService"
    class="initMethod.StudentServiceImpl"
    init-method="subscribe"
    destroy-method="unsubscribe"/>



2. Implementing InitializingBean and DisposableBean interfaces
The bean can implement these methods InitializingBean.afterPropertiesSet() and DisposableBean.destroy(). Like so:

public class StudentServiceImpl implements StudentService, InitializingBean, DisposableBean {
    @Override
    public boolean isExists(long studentId) {
        // A fake implementation
        if ( studentId % 2 == 0 )
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception 
    {
        // subscribe logic goes here
    }

    @Override
    public void destroy() throws Exception 
    {
       // unsubscribe logic goes here
    }
}



3. Using @PostConstruct and @PreDestroy Annotations
Note that both of these are JSR-250 annotations (here is a nice introduction to JSR-250 support introduced in spring 2.5).

@PostConstruct
public void subscribe()
{
  // subscribe logic goes here
}


@PreDestroy
public void unsubscribe()
{
  // unsubscribe logic goes here
}

Wednesday, September 28, 2011

If the work is worthwhile, then whether we can complete it or not, it's worth making the attempt

If the work is worthwhile, then whether we can complete it or not, it's worth making the attempt. That's why courage is important.
- Dalai Lama

Actions driven solely by anger are of no use at all

Actions driven solely by anger are of no use at all; realizing this can help strengthen your determination to resist it. - DalaiLama

Wednesday, September 21, 2011

Monday, September 19, 2011

One hand

"We are afraid of losing what we have, whether it's our life or our possessions and property. But this fear evaporates when we understand that our life stories and the history of the world were written by the same hand." Sometimes, the caravan (in the middle of a desert) met with another. One always had something that the other needed - as if everything were indeed written by one hand. - The Alchemist by Paulo Coelho

No matter how many detours and adjustments it made, the caravan moved towards the oasis.

"The desert was all sand in some stretches, and rocky in others. When the caravan was blocked by a boulder, it had to go around it; if there was a large rocky area, they had to make a major detour. If the sand was too fine for the animal's hooves, they sought a way where the sand was more substantial... If a guide were to fall ill or die, the camel driver would draw lots and appoint a new one. But all this happened for one basic reason: no matter how many detours and adjustments it made, the caravan moved towards the same compass point. Once obstacles were overcome, it returned to its course, sighting on a star that indicated the location of the oasis." - The Alchemist by Paulo Coelho

Tuesday, September 13, 2011

Don’t ask yourself what the world needs, ask yourself what makes you come alive

“Don’t ask yourself what the world needs, ask yourself what makes you come alive. And then go and do that. Because what the world needs is people who are alive.” - Howard Thurman through Think Simple Now

Friday, September 02, 2011

Saturday, August 20, 2011

I am a parent and do not have the luxury of principles.

Father (who doesn't like his state going to war): I am a parent and do not have the luxury of principles.
...
Son (who is enlisting in the army): Father, I thought you were a man of principles.

Father: When you have a family of your own perhaps you will understand.

Son: When I have a family of my own I won't hide behind them.

- "The Patriot"

Wednesday, August 10, 2011

when each day is the same as the next

"...when each day is the same as the next, it's because people fail to
recognize the good things that happen in their lives every day that the
sun rises."

"The Alchemist" by Paulo Coelho

Thursday, August 04, 2011

Money for good books

"No matter how tight our budget was over the years, we always made money available for good books." - Tim seldin in "How to raise an amazing child The montessori way"
Published with Blogger-droid v1.6.8

Monday, July 18, 2011

There isn’t a way things should be. There’s just what happens, and what we do.

“There isn’t a way things should be. There’s just what happens, and what we do.”

- “A Hat Full of Sky ” by Terry Pratchett through Julie911

I don’t know the key to success, but the key to failure is trying to please everyone.

“I don’t know the key to success, but the key to failure is trying to please everyone.”

- Bill Cosby through Julie911

Friday, July 08, 2011

This is your life. Learn to enjoy what you’ve got.

“This is your life. Learn to enjoy what you’ve got.”

- Mr. Destiny through Julie911