Monday, April 19, 2010

How to implement BeerPool through counting semaphore?

No. I am not going to write code here. Drawing class diagram is also tough here. So let me list down the requirements and solution through text as much as possible.

1. It should always have the minimum beer bottles in the initialized state. i.e filled state.
2. Beer bottles should not exceed the maximum count.
3. Beer bottles should be reused (irrespective of whether they are cleaned or not)

Now one way of doing the same is listed below

A skeleton of the code is given below

public class BeerPool
{
public BeerPool getInstance()
public BeerBottle getBeerBottle()
private BeerBottle createBeerBottle()
public BeerBottle freeBeerBottle()
}

Let me maintain an integer createdBeerCount that takes care of the number of created beer bottles. getBeerBottle and freeBeerBottle methods are synchronized where you will be incrementing and decrementing counting semaphore.

LinkedList is used to maintain the BeerBottles. In getBeerBottle() method, we will use pop method of the LinkedList and return that object.

IMPORTANT: point to note here is removeElementAt(0), is O(1) operation. If createdBeerBottleCount is >= maxBeerBottleCount, thread has to wait. On interruption throw timeout exception.

Now on freeBeerBottle() method, reduce the count, and push returned BeerBottle in the LinkedList as last object. This method also takes O(1) time.

Since you know the MAX_BEER_BOTTLES count, you could easily use LinkedList instead of any other collection. But the key point here is we need to add and remove elements in O(1) time.

Saturday, April 17, 2010

What is the difference between mutex, monitor, lock and semaphore?

Mutex is a lock. At any point of time only one thread can hold that lock and once its job is done, it has to acknowledge that through unlocking mechanism. Its used for mutual exclusion to avoid multiple threads corrupting global datasource. Java's synchronized keyword does this internally.

Semaphores are useful in sharing common resources efficiently through counters. For example object pooling and thread pooling can be implemented through counting semaphores. By functionality binary semaphore is equivalent to mutex except the fact that monitor ownership is provided in the mutex case. In java, semaphores can be implemented through wait and notify mechanism.

Lock shall be implemented using Monitor.  Monitor and Lock are giving same functionality. Across threads mutual exclusion can be implemented using Monitors. Across processes mutual exclusion can be implemented using mutex.

Have you ever wondered the difference between notify and notifyAll?

Have you ever wondered difference between notify and notifyAll methods in object?

Notify notifies only one thread that is waiting on the object. NotifyAll notifies all the threads that are waiting on the object.

Now what happens when more than one thread is waiting on the same object and notify is invoked?

Only one of these waiting thread will be entering into running state and all the other threads will be in waiting state. On the other hand, in you invoke notifyAll, all the threads will be entering into running state one by one.

Monday, April 12, 2010

Missing Number

In a table you have 99999 rows. But the max id is 100000. That means only one row is missing. How will you find that missing id in one simple query?

















select (max(id)*(max(id)+1))/2 - sum(id) from table;

Math: to find sum of n integers, you can use n*n+1/2.

Tuesday, April 6, 2010

Expectations Series 1 - From a Software Engineer

Year 0: Getting a job in MNC during campus interview
Year 1: What bike to choose?
Year 2: When is my next hike?
Year 3: When I will get Onsite? Which project should I choose to get onsite?
Year 4: Will SHE come for onsite?
Year 5: In which area should I buy plots?
Year 6: Is my domain safe from pay cut-off & recession?
Year 7: Boring work.....should I change the domain or company?
Year 8: Which girl to choose?

Coming more.....

Friday, April 2, 2010

Little things in Travel

1. A thank you with a smile for the bus driver after a long safe night journey.
2. Taking ticket for the co-passenger when conductor is angry with no-change. he he it happened to me.
3. Girl asking a guy/man to sit near her when there are no common seats are available. Especially in tamilnadu buses.
4. When a grandfather like man around 70 years of age inquires about today's sachin's score to an unknown young boy sitting next to him.
5. Taking care of a child from a standing mother. I happened to see that in all buses.
6. Conductor waking you up at the right place when you are asleep in between in your long travel.
7. Train ticket collector finishing his duties when your turn arrives.
8. Conductor giving exact change when you have no change in your wallet.
9. Seeing a known person when you have zero money in your wallet to get the ticket.
10. Stopping bus, seeing your hand signal.
add more...

Thursday, April 1, 2010

Title Reason - Where is devil?

Where is Devil?













Devil is in your mind. If you feel happy for simple cute things then you will make others happy for such things. Be it a knotty joke or past experience or a comedy scene in a movie, enjoy it and give the pleasure to others. Spread your positive attitude in professional and personal life. A simple cute action or word can change entire tensed situation as a simple pleasant one. For example saying "I Love you" to your wife after 10 years of tiring marriage life.


Now what are all the cute little things that make me happy and satisfied? Later ..