Wednesday, March 28, 2007

Quote for the Day

A good listener is not only popular everywhere, but after a while he gets to know something.

- Wilson Mizner

Ways the Threads leaving Running State

The ways by which a running thread could leave the running state :

1.When a sleep() method is called.
2.When a yield() method is called.
3.When a join() method is called.
4.When a wait() method is called.
5.When the run() method is completed.
6.When its blocked ,waiting for some resources.
7.When the Thread Scheduler Schedules the Threads.

* A call to sleep() Guaranteed to cause the current thread to stop executing for at least the specified sleep duration (although it might be interrupted before its specified time).

* A call to yield() Not guaranteed to do much of anything, although typically it will cause the currently running thread to move back to runnable so that a thread of the same priority can have a chance.

* A call to join() Guaranteed to cause the current thread to stop executing until the thread it joins with (in other words, the thread it calls join() on) completes, or if the thread it's trying to join with is not alive, however, the current thread won't need to back out.

Thread States

The Life cycle or States of Threads.

1.New
2.Runnable
3.Running
4.Sleeping/waiting/blocked (Not Runnable)
5.Dead

Once the start() method is called, the thread is considered to be alive . A thread is considered dead after the run() method completes.

The isAlive() method is the best way to determine if a thread has been started but has not yet completed its run() method.

New state - when Thread is instantiated , but before the start method is invoked.

Runnable state - when the start method is invoked , and its ready to execution

Running state - This is where the action takes place.

Not Runnable state - This is where , the Thread is still alive ,but not eligible to run.A thread arrives at this state ,when it is blocked waiting for a resource like I/O , or when the sleep method is called or when the wait method is called.

Dead state - A thread is considered dead when its run() method completes.

Tuesday, March 27, 2007

Difference between potentially and reality

Youngest Son: Daddy, what is the difference between "potentially" and" in reality"?

Dad: "I will demonstrate"

Dad turns to Wife and asks her: Would you sleep with Robert Redfordfor 1 million dollars?

Wife: Yes of course! I would never waste such an opportunity!

Then Dad asks his daughter if she would sleep with Brad Pitt for 1million dollars?

Daughter: Waow! Yes! This is my fantasy!

Dad next turns to his elder son and asks him: Would you sleep with TomCruise for 1 million dollars?

Elder Son: Yeah! Why not? Imagine what I could do with 1 milliondollars! I would never hesitate!

So Dad then turns back to his younger son saying:

You see son, "potentially" we are sitting on 3 million dollars, but"in reality" we are living with 2 Bitches and a Gay.

Important facts of Threads

* Each thread will start, and each thread will run to completion.
* A thread is done being a thread when its target run() method completes.
* Once a thread has been started, it can never be started again.
* The order in which runnable threads are chosen to run is not guaranteed.

Instantiating, and Starting Threads

The java.lang.Thread class contains the methods to create,start and pause threads.Some basic methods to be known from the Thread class are

1.start()
2.yield()
3.sleep()
4.run()

The 'work to be done' is always put in the run() method.

You can instantiate the thread by two ways.

1.By extending the java.lang.Thread class
2.By implementing the Runnable interface

Mostly you will be using the second one which is a good OO practise.Since java does not allow multiple inheritance , and if you use the first method, you cant inherit from other class.And its also good to extend the java.lang.Thread , only if the class needs to have specialized thread like behaviour.


Constructors in Thread:

1.Thread()
2.Thread(string)
3.Thread(Runnable)
4.THread(Runnable,String)

Where string - Name of the thread , Runnable - Instance of the class that implements Runnable

Even though you instantiate the thread , it doesnot enter the runnable state. In order to run the thread , we need to call the thread.start() method.

For example , when you extend a Thread ,syntax is

Thread t1=new Thread();
t1.start();

Eventhough you implement the Runnable interface , an instance of java.lang.Thread is needed to run the thread , by using the thread.start() method.

For example , when you implement the Runnable interface ,

The following is legal ,but does not start a separate thread

ClassName r = new ClassName();
r.run();

(ClassName -The one that implements Runnable interface)

In order to start the thread , replace the above code by below one.

ClassName obj=new ClassName();
Thread t1=new Thread(obj);
t1.start();


As soon as start() method is called , the following takes place.

* A new thread of execution starts (with a new call stack).
* The thread moves from the new state to the runnable state.
* When the thread gets a chance to execute, its target run() method will run.

Threads Concept

Thread is a light weight process. In java Thread can be said as

1.An instance of class java.lang.Thread
2.A thread of execution

An Instance of Thread is just like anyother objects in java , that has variables and methods , and lives and dies on the heap.

Whereas 'thread of execution' is a light weight process that has its own call stack.

That is ,'instance of class Thread' is related to 'worker' and 'a thread of execution' is related to 'the work to be done'.

From this, it is known that , you need a worker to do the work.So the thread is the worker and the work to be done is present in the run() method.

In java there is one thread per call stack and vice-versa.

Even if you dont create any new threads in your peogram , threads are there running in the back.For example , the main() method runs in one thread called main thread.

We have heared that threads are running concurrently or parallely.We also have heared that processor executes one thing at a time.In which case , both are contradictory.

Here comes the concept of scheduling . What actually happens is ,JVM gets its turn at the CPU by whatever scheduling mechanism the underlying OS uses, and operates like a mini-OS and schedules its own threads regardless of the underlying operating system. In some JVMs, the Java threads are actually mapped to native OS threads.

So, "When it comes to threads, very little is guaranteed".That is ,the behaviour in which the threads work will differ from machine to machine.

A Good Survey

A Worldwide survey was conducted by the UN. The only question asked was: "Would you please give your honest opinion about solutions to the food shortage in the rest of the world?"

The survey was a huge failure.

The reason:

In Africa they didn't know what 'food' meant, In India they didn't know what 'honest' meant, In Europe they didn't know what 'shortage' meant, In China they didn't know what 'opinion' meant, In the Middle East they didn't know what 'solution' meant, In South America they didn't know what 'please' meant, And in the USA they didn't know what 'the rest of the world' meant!!

Monday, March 26, 2007

Difference final,finally and finalize

* final - constant declaration.

* finally - The finally block always executes when the try block exits, except System.exit(0) call. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

* finalize() - method helps in garbage collection. A method that is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these non-memory resources through the finalize() method.

Garbage Collection

Garbage Collection is not about destroying Objects.
Its all about managing memmory.The reason for the existence of the garbage collector is to recover memory that your program is no longer using.
You cannot force garbage collection to take place.

However calling Sysem.gc() is a "hint" to the runtime engine that , now might be a good time to run the GC. Some implementations take that hint to heart and some don't.

Marker Interface

One of the features of the Java programming language is that it mandates a separation between interfaces (pure behavior) and classes (state and behavior). Interfaces are used in Java to specify the behavior of derived classes. Often you will come across interfaces in Java that have no behavior. In other words, they are just empty interface definitions. These are known
as marker interfaces.

Some examples of marker interfaces in the Java API include:
* java,lang.Cloneable
* java,io.Serializable

* java.util.EventListener,etc.

Marker interfaces are also called "tag" interfaces since they tag all the derived classes into a category based on their purpose.

nth highest or lowest value in sql

To fetch the nth highest or lowest value in sql.

For example To fetch the 4th highest value , replace 1 by 4 in the below query.

select * from test e where 1=(select count(distinct sal) from test where e.sal>=sal);

To fetch the 4th lowest value , replace 1 by 4 and replace the '>' with '<' in the above query.

Normalization

what is Normalization?

It's the process of efficiently organizing data in a database.

Goals of the Normalization:

1.Eliminate redundant data
2.Ensure data dependencies make sense

The database community has developed a series of guidelines for ensuring that databases are normalized. These are referred to as normal forms and are numbered from one (the lowest form of normalization, referred to as first normal form or 1NF) through five (fifth normal form or 5NF).

In practical applications, you'll often see 1NF, 2NF, and 3NF along with the occasional 4NF.
Fifth normal form is very rarely seen.

First normal form (1NF) sets the very basic rules for an organized database:

* Eliminate duplicative columns from the same table.
* Create separate tables for each group of related data and identify each row with a unique column or set of columns (the primary key).

for examples click here

Second normal form (2NF) further addresses the concept of removing duplicative data:

* Meet all the requirements of the first normal form.
* Remove subsets of data that apply to multiple rows of a table and place them in separate tables.
* Create relationships between these new tables and their predecessors through the use of foreign keys.

for examples click here

Third normal form (3NF) goes one large step further:

* Meet all the requirements of the second normal form. * Remove columns that are not dependent upon the primary key.

for examples click here

Finally, fourth normal form (4NF) has one additional requirement:

* Meet all the requirements of the third normal form.
* A relation is in 4NF if it has no multi-valued dependencies.

Tuesday, March 20, 2007

Quote for the Day

"I am not afraid of one who knows 10000 kicks.But i am afraid of one who has practised one kick 10000 times."

-Bruce Lee.

Constructors in Java

Few points about constructors :

It has no return type.

It can use any access modifier, including private.

Its name must match the name of the class.

You could have both a method and a constructor with the same name in the same class.

Constructors are never inherited.

The default constructor is always a no-argument one.That is , it doesnot have arguments.

It can't be overridden.

It can be overloaded.

You cannot invoke an instance (in other words, nonstatic) method (or access an instance variable) until after the super constructor has run.

Key Rule: The first line in a constructor Must be a call to super() or a call to this().

Life Cycle of IT Companies

Programmer to Team Leader :

"We can't do this proposed project.**CAN NOT**. It will involve a major design change and no one in our team knows the design of this legacy system.And above that, nobody in our company knows the language in which this application has been written. So even if somebody wants to work on it,they can't. If you ask my personal opinion, the company should never take these type of projects."

Team Leader to Project Manager :

"This project will involve a design change. Currently, we don't have any staff who has experience in this type of work. Also, the language is unfamiliar to us, so we will have to arrange for some training if we take this project. In my personal opinion, we are not ready to take on a project of this nature."

Project Manager to 1st Level Manager :

"This project involves a design change in the system and we don't have much experience in that area. Also, not many people in our company are appropriately trained for it. In my personal opinion, we might be able to do the project but we would need more time than usual to complete it."

1st Level Manager to Senior Level Manager :

"This project involves design re-engineering. We have some people who have worked in this area and others who know the implementation language. So they can train other people. In my personal opinion we should take this project,but with caution."

Senior Level Manager to CEO :

"This project will demonstrate to the industry our capabilities in remodelling the design of a complete legacy system. We have all the necessary skills and people to execute this project successfully. Somepeople have already given in house training in this area to other staff members. In my personal opinion, we should not let this project slip by us under any circumstances."

CEO to Client :

"This is the type of project in which our company specializes. We have executed many projects of the same nature for many large clients. Trust me when I say that we are the most competent firm in the industry for doing this kind of work. It is my personal opinion that we can execute this project successfully and well within the given time frame".

MBA Vs B.E

A MBA and a B.E go on a camping trip, set up their tent,and fell asleep.

Some hours later, the B.E Guy wakes his MBA friend. " look up at the sky and tell me what you see."

The MBA replies, "I see millions of stars."

"What does that tell you?" asks B.E Student.

The MBA ponders for a minute.

"Astronomically speaking, it tells me that there aremillions of galaxies and potentially billions of planets.

Astrologically, it tells me that Saturn is in Leo.Time wise, it appears to be approximately a quarter past three.

Theologically, it's evident the Lord is all-powerful andwe are small and insignificant.

Meteorologically, it seems we will have a beautiful day tomorrow. "

What do you think ?" asks MBA student.

The B.E is silent for a moment, then speaks.

"Practically...Someone has stolen our Tent "

Friday, March 16, 2007

Quote for the Day

There are two kinds of people,those who do the work and those who take the credit.Try to be in the first group;there is less competition over there.

- Indira Gandhi

Thursday, March 15, 2007

Few Definitions

School: A place where Papa pays and Son plays.

Life Insurance: A contract that keeps you poor all your life so thatyou can die Rich.

Nurse: A person who wakes u up to give you sleeping pills.

Marriage: It's an agreement in which a man loses his bachelor degreeand a woman gains her masters.

Divorce: Future tense of Marriage.

Tears: The hydraulic force by which masculine willpower is defeated byfeminine waterpower.

Lecture: An art of transferring information from the notes of theLecturer to the notes of the students without passing through "the minds ofeither"

Conference: The confusion of one man multiplied by the number present.

Compromise: The art of dividing a cake in such a way that everybodybelieves he got the biggest piece.

Dictionary : A place where success comes before work.

Conference Room : A place where everybody talks, nobody listens andeverybody disagrees later on.

Father: A banker provided by nature.

Criminal: A guy no different from the rest....except that he gotcaught.

Boss: Someone who is early when you are late and late when you areearly.

Politician : One who shakes your hand before elections and yourConfidence after.

Doctor : A person who kills your ills by pills, and kills you by bills.

Classic: Books, which people praise, but do not read.

Smile: A curve that can set a lot of things straight.

Office: A place where you can relax after your strenuous home life.

Yawn: The only time some married men ever get to open their mouth.

Etc.: A sign to make others believe that you know more than youactually do.

Committee : Individuals who can do nothing individually and sit todecide that nothing can be done together.

Experience: The name men give to their mistakes.

Atom Bomb: An invention to end all inventions.

Philosopher: A fool who torments himself during life, to be spoken of whendead

Difference between Overloading and Overriding

Though its easy to determine whether Overloading or overriding takes place , using the argumentlist,return types,access specifiers and exceptions.We do confuse while invoking the methods using reference type and object type.

In case of overloading ,Reference type determines which method is selected from which class.In case of overriding ,Object type determines which which method is selected from which class.

Overloading happens during compile time,while overriding happens during Runtime.

Inheritance Concept

Use of Inheritance

* To promote code reuse
* To use polymorphism

Inheritance in java is done by either extending a class or implementing the interfaces.

Inheritance is all about having a parent child relationship.So instead of rewriting the same functionality several times , you can just inhert those from the parent class.

Inheritance relationship can be of two types.

IS-A Relationship

Has-A Relationship

Is-A relationship is said to be direct relationship and can be done by either extending the parent class or by implementing the interface as said earlier.

For example

class Vehicle{}

class car extends vehicle{}

class ford extends car{}

In this case ford IS-A car . car IS-A Vehicle . So in the class Vehicle we can define several properties and functionalities that are similar , and declare several functionalities as abstract so that they can be overriden by the subclasses that inherit them.Since we have abstract methods in Vehicle class , we need to declare Vehicle as a abstract class.


Has-A relationship is said to be indirect relationship , in the sense , you dont want to extend or implement anything , rather create a instance of the class you want to inherit , and use that instance to invoke its methods.In this way the user dont have to know anything about the inherited class.For him,it seems like the functionality is in his Class.

for example

class Animal{}

class Halter { void tie(Rope r) { } }

class horse extends Animal {
Halter myhalter;
void tie(Rope r) { myhalter.tie(Rope r); }
}

In this case horse IS-A Animal.But Halter is not a Animal.Its just a equipment used to tie the horse. So horse Has-A Halter.Though horse doesnot extend or implement Halter it can use the 'tie' functionality by creating a instance of the Halter class.This is done as shown above.Now it seems like , we are invoking the 'tie' functionality of the horse, which in turn invokes the 'tie' functionality of Halter Class.so the actual implementation goes in Halter class and the user need not be aware of what is happening in the Halter class.For him its the tie method in the horse class is performing the operations.

Inheritence Using Abstract Classes and Normal classes

While playing with minimum of 1 or 2 classes its easy to implement the concept of inheritence using concrete(normal) classes.

But in case of inheriting properties within a hierarchy or family , its better to use Abstract classes , so that you can define some functionalities that is same throughout the hierarchy and you can override several functionalities so that its more specific to that subclass.

So only the circumstance differ and not the concept of inheritence.

Monday, March 12, 2007

Good Moral

A man feared his wife wasn't hearing as well as she used to , and he thought she might need a hearing aid. Not quite sure how to approach Her, he called the family Doctor to discuss the problem.

The Doctor told him there is a simple informal test the husband could perform to give the Doctor a better idea about her hearing Loss.

Here's what you do," said the Doctor,

"stand about 40 feet away from Her, and in a normal conversational speaking tone see if she hears You. If not, go to 30 feet, then 20 feet, and so on until you get a Response.

" That evening, wife was in the kitchen cooking dinner, and he was in the den. He says to himself, "I'm about 40 feet away, let's see what happens."

Then in a normal tone he asks,

'Honey, what's for Dinner?" No response.

So the husband moves closer to the kitchen,about 30 feet from his wife and repeats,

"Honey, what's for dinner?" Still no response.

Next he moves into the dining room where he is about 20 feet from his wife and asks,

"Honey,what's for dinner?" Again he gets no Response


So He walks up to the kitchen door, about 10 feet away.

"Honey, what's for dinner?" Again there is no response.

So he walks right up Behind her.

"Honey, what's for dinner?" ;;;;"James, for the

FIFTH time I've Said, CHICKEN!"


Moral : The problem may not be with the other one as we always think, could Be very much within us ..!

Terrorists and Project Manager

Employees of Software Company are all worried.
Some are roaming around. Some are in loud
discussions during office time.....

Some Trainees, who had just joined, notice this
and enquire about what happened to a senior
employee, they ask, "What's going on?"

"Terrorists have kidnapped our Project Manager."

They're asking for Rs.500 Crores ransom,
otherwise they're going to douse them with petrol
and set them on fire.

We're going from desk to desk, taking up a
collection."

One Trainee asks, "How much is everyone giving,
on average?................................................

"About a litre."

Wednesday, March 7, 2007

Thats Life

Arthur Ashe, the legendary Wimbledon player was dying of AIDS which he got due to infected blood he received during a heart surgery in 1983.

From world over, he received letters from his fans, one of which conveyed: "Why does GOD have to select you for such a bad disease"?

To this Arthur Ashe replied: The world over -- 5 million children startplaying tennis, 50 thousand learn to play tennis, 5 thousand learn professional tennis, 50,000 come to the circuit, 5000 reach the grand slam, 50 reach Wimbledon, 4 to semi final, 2 to the finals, When I was holding a cup I never asked GOD "Why me?". And today in pain I should not be asking GOD "Why me?"

Happiness keeps u Sweet, Trials keep u Strong, Sorrow keeps u Human,Failure Keeps u Humble, Success keeps u Glowing, But only God Keeps uGoing.....
Keep Going

MARKETING CONCEPTS

There can never be a better way of teaching Marketing Concepts than this.......

1. You see a pretty girl at a party. You go up to her and say: ''I'm very rich. Marry me!" That's Direct Marketing.

2. You're at a party with a bunch of friends and see a pretty girl. One of your friends goes up to her and pointing at you says: ''He's very rich, Marry him." That's Advertising.

3. You see a pretty girl at a party. You go upto her and get her telephone number.The next day, you call and say: ''Hi, I'm very rich. Marry me." That's Tele marketing.

4. You're at a party and see a pretty girl. You get up and straighten your tie, you walk to her and pour her a drink, you open the door of the car for her, pick up her bag after she drops it, offer her ride and then say: "By the way, I'm rich. Will you marry me?" That's Public Relations.

5. You're at a party and see a pretty girl.She walks up to you and says: "You are very rich! Can you marry! Me?" That's Brand Recognition.

6. You see a pretty girl at a party. You go up to her and say: "I am very rich. Marry me!" and she gives you a nice tight slap on your face. That's Customer Feedback.

7. You see a pretty girl at a party. You go up to her and say: "I am very rich. Marry me!" And she introduces you to her husband. That's demand and supply gap.

8. You see a pretty girl at a party. You go up to her and before you say anything, another person come and tell her: I'm rich.will you marry me?", and she goes with him. That's competition eating into your market share.

9. You see a gorgeous girl at a party. You go up to her and before you say: "I'm rich, Marry me!", your wife arrives. That's restriction for entering new markets.

Tuesday, March 6, 2007

Encapsulation

It is a process of hiding the implementation details from other classes and just providing a way to access and modify its data.So that ,you can make necessary changes or corrections to your code without affecting or changing the classes that inherits your class.

Consider a situation, where you write a class and your friends inherit it. your class consist of a instance variable declared public as shown below.

class your{
public int size;
}
Now, your friend assigns a value to size , that produces a error in your code.

class friends{
yours y=new yours();
yours.size=-32;
}

Though assigning -32 to size is legal, its bad for you.Because you would be expecting positive values alone. So what will you do now? you would write a setter method that check the value and assigns it to size.

So, there is a need to change your code , as well as your friends code to access the method you have implemented in your class.

This where Encapsulation comes in place.

Benefit of Encapsulation

The ability to make changes in your implementation code without breaking the code of others who use your code is a key benefit of encapsulation.Thus bringing in the Flexibility and Maintainability,and scalability.

In order to do that, Declare your instance variables as private. provide methods to get and set the values of the variable ,like something present in javabeans.

Now lets rewrite your class, so that it is flexible , maintainable , and scalable.

class yours{
private int size;
public int getSize() { return size; }
public void setSize(int x) { //code for checking whether x is positive size=x; }
}

Not only checking, you can also do several other operations, whithout affecting other classes that inherits it.

Monday, March 5, 2007

Second Max value - query

Its often asked in interviews , "Write a query to retrive the second maximum salary from a table." , especially when we are questioned about Databases.Here is a solution...

Select max(salary) from tablename where salary not in(select max(salary) from tablename);

or

select max(salary) from tablename where salary<(select max(salary) from tablename);

BlackBerry - Wireless Solution

What Is BlackBerry?

BlackBerry offers leading wireless solutions, providing access to a wide range of applications on a variety of BlackBerry devices, as well as BlackBerry enabled devices around the world. BlackBerry wireless solutions combine wireless devices with software and services to keep mobile professionals connected to the people, data and resources that drive their day.

Applications and Services
* Wireless Email
* Phone
* Internet
* Organizer Applications*

Thursday, March 1, 2007

Its all upto YOU

When someone is doing something or about to do something, in a way you don't want it to be done - and you are not able to accept it - you become angry.

When someone is doing something or about to do something, in a way you don't want it to be done - and you are able to accept it - you remain tolerant.

When someone is having something or someone is able to produce the results which you are not able to produce - and you are not able to accept it - you become jealous.

When someone is having something or someone is able to produce the results which you are not able to produce - and you are able to accept it - you get inspired.

When you are encountering uncertainty or is about to encounter uncertainty, which you are not sure how you are going to handle - and you are not able to accept it - it causes fear in you.

When you are encountering uncertainty or is about to encounter uncertainty, which you are not sure how you are going to handle - and you are able to accept it - you feel adventurous about it.

When someone has done something that has emotionally hurt you - and you are not able to accept it - it develops hatred in you.

When someone has done something that has emotionally hurt you - and you are able to accept it - it helps you forgive them.

When someone is present in your thoughts, but is not physically present - and you are not able to accept it - you say 'i m missing u'.

When someone is present in your thoughts, but is not physically present - and you are able to accept it - you say 'i m thinkin of u'.

Thus, Emotional Equation becomes:

Something + acceptance = positive emotion
Something + non-acceptance = negative emotion

Morale : So, it is not 'something' or 'someone' who is making you feel positive or negative, but it's your 'acceptance' or 'non-acceptance' of something or someone, which impacts things.