Friday, March 18, 2011

Thread pool in java


In large scale application, multithreading is commonly used to execute independent tasks. Now imagine 100 classes extending the Thread class (or implementing the runnable interface) and each one having its own run method and you are asked to write those run methods. Obviously, you need to repeat the same code and managing that code even becomes more difficult.
A very simple solution to this is to build a thread pool where in you will have a queue of objects on which the run method will be called. Any pre-pocessing will be done in the objects own member methods. Its only that the tasks will be added to the queue and an infinite loop will look for yet to run threads and will run them. You need two queues, one containing the threads that are currently running and other containing those yet to be run.
These queues can be LinkedLists.

1) Maximum and minimum number of threads pending for execution can be controlled.
2) Maximum alive time for each thread can be controlled
3) Many other business requirements in terms of multithreading can be achieved at a central point.
4) Your application’s multithreading is segregated which makes it look decent and well managed and gives a good impression to the reviewer.
5) Your life becomes much easier.


How to achieve it:
1) Create a class to maintain the threadpool information like the read and running queues, the variables for max and min number of threads, time to keep a thread alive
2) A class which contains the loop to check for threads in the running and ready queues and make a decision whether to invoke a particular thread or not.

Abstract Classes,Interfaces and extends


1) An interface can’t implement another interface
2) An interface can extend another interface
3) An interface can extend multiple interfaces. (known as super interface)
4) An abstract class can implement an interface
5) An abstract class can extend another abstract class
6) An interface can’t extend an abstract class



 
some java tips


1) switch expects an argument of int type. But due to auto casting, short, byte, char also work.
2) strictfp Used in front of a method or class to indicate that
floating-point numbers will follow FP-strict rules in all expressions.
3) Even the order of arguments matter when overriding a method
4) this and super are special kind of keywords
5) object references and local variables are stored on stack.
6) Is a relationship is inheritance and HAS a relationship is a composition (One class has an object of other as its member e.g A car has an engine)



Monday, March 14, 2011

Difference between Hashmap and Hashtable


1) Hashtable and Hashmap are different in the sense that Hashmap is synchronized and permits nulls as keys as well as values. Hashtable requires non-null values for keys as well as values.
a. In case iteration performance is important, keep the capacity/size of hashmap small because the time for iteration is proportional to the capacity + size. However, get() and put() have constant time performance.
b. The load factor and initial capacity effect the performance of the hashmap. When the number of entries in the hashmap exceed the product of load factor and current capcity, the hashmap is rehashed so as to have twice the number of buckets. A value of 0.75 for load factor is good tradeoff between time and space cost.
c. The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.
d. The parameterized constructors are allow for specifying the initial capacity and/or load factor.
e. The hashmap is not synchronized for structural changes (addition and deletion of keys) and not for operations like updating the value for a key. Collections.synchronizedMap should be used for synchronized access.
f. The iterator returned is fail-fast i.e. as soon as the map is structurally modified using other than iterator’s remove method, the iterator throws ConcurrentModificationException. However, the fail-fast behaviour is not guaranteed.
g. The put method returns null if either there was no such key in the map or the value associated with the key was null or it returns the previous value associated with key, if it exists in the map.
h. Similar is the case with get(), it returns null if either there is no key with name specified or the key explicitly maps to null. The containsKey can be used to distinguish these two cases. 

Saturday, March 12, 2011

java access modifiers


1) A top level class can have public, abstract and final as access modifiers. To make the class default do not specify any access modifier.
2) A top level interface can have public and abstract as access modifier
3) The string class is a final class. Final classes can improve performance as the methods are expanded inline.
4) All the methods of final class are also final.
5) Transient keyword can only be applied to member variables and it makes those variable non-persistable while serializing the object.
6) Java interfaces cannot have transient variables.
7) Volatile variables maintain a master copy in addition to the local copy of each thread and each thread has to keep its copy in sync with the local copy. They are useful to avoid concurrency problem when synchronization is not an option. They are somewhat slower.
8) The only modifier that can be applied to local variables in final.

Friday, March 11, 2011

MyJourney


I started after everyone else did, on the road from one of the farthest locations. The road is said to be the path of glory, a proven way to success. I didn't know what my exact destination was. It is said that wherever the road goes, it leads you to a much sought after place. So I never thought of the destination. My only aim was to travel along the path without any diversions.

I started walking at a slower pace. Initially I felt it difficult moving on the road. Gradually I found comfort in moving forward. With every step I advanced, I felt my speed was increasing. To support that I crossed every milestone faster than I did the previous one.

One by one, I started overtaking people who started before me. I never dreamt of the destination I was headed towards. It was not my love for the destination, but the strong will to stay ahead on the road, that drove me. In no time I went past everyone I could see and was leading on the way. It was then I saw a mountain across the road.

I didn't find any difficulty in climbing up the mountain and I reached the cliff with ease, where I was astounished by the liveliness of the place. It was the most blissful and beautiful place I have ever seen or been. And on the cliff, not out of tiredness but for the solace I found in the place, I fell asleep.

In the sleep, I had a dream. The dream was simple. The dream was a fable. The dream was one I felt very real. In the dream, I saw superheroes withour super powers, fairies without wings, magicians with out wands, warriors without weapons and all. I made friends with them. I fought with them. I sang with them. I danced with them. I played with them.. And I learnt that I was one of them. I cried for them. I laughed at them. I hated them. I loved them. And I woke up.

The dream was so fascinating that it confused me. It questioned me about my destination. The people who I overtook almost reached me and started going past me. I didn't feel bad about that. I knew that I could easily gain my lead, if I walk at my usual speed.  I resumed my travel. The dream was so powerful that I walked as if I was under a spell. The dream was haunting. I wished that my dream was true. I wished that this road took me to my dream. The dream so enchanting that I decided that the dream was my destination.

For the first time, I felt a faint view of my destination in my thoughts. Every step I went forward, my love for the destination increased. But with the every step, I felt like I was moving away from my destination. After going for some distance at an ordinary pace, I found that my love for the destination surpassed my will to walk on the road. I wondered whether the road would ever reach me to my destination.

There might have been some other path to my destination, I thought. My aim was no more to walk on the proven path, it was to reach my destination. There might not be a well constructed path to my dream. I wanted to explore it. There might not even be a path to reach the dream. I wanted to construct it. And I took a diversion.

when does the diversion come to an end?the next question to myself...It was my awesome experience working with GOOGLE.....now constructing the new path again with a great hope of success