Tuesday, May 10, 2011

Threads In JAVA.............

QUESTIONS
1. Write a Java program to create three threads namely A, B, C and make them run one after another. (C has to run after B completes, B has to run after A completes).
2. What happens when a thread calls notify() but no thread(s) is waiting?
3. How will you declare a timer variable that will be accessed by multiple threads very frequently?
4. How will you synchronize static variables?
5. What happens when threads are waiting, but never being notified?
ANSWERS:
1. Many ways to do it. I prefer join(). When a thrad A calls b.join(), then A runs only after thread b completes. So, for this problem, C has to call b.join() and B has to call a.join()
2. Actually…, nothing :) the notify() call simply returns.
3. Declare the variable as volatile. Every thread caches a copy of instance variable, work with local copy, and then sync it with master copy. But, threads do not cache a copy of volatile instance variable.
4. Obtain class level lock. synchronized( obj.getClass()) {……..}
5. Leaving the answer to readers! :)

serialVersionUID


Give the use and an example of the scenario where you would use serialVersionUID in a your Java class?

Ans) During object serialization, the default Java serialization mechanism writes the metadata about the object, which includes the class name, field names and types, and superclass. This class definition is stored as a part of the serialized object. This stored metadata enables the deserialization process to reconstitute the objects and map the stream data into the class attributes with the appropriate type
Everytime an object is serialized the java serialization mechanism automatically computes a hash value. ObjectStreamClass’s computeSerialVersionUID() method passes the class name, sorted member names, modifiers, and interfaces to the secure hash algorithm (SHA), which returns a hash value.The serialVersionUID is also called suid.
So when the serilaize object is retrieved , the JVM first evaluates the suid of the serialized class and compares the suid value with the one of the object. If the suid values match then the object is said to be compatible with the class and hence it is de-serialized. If not InvalidClassException exception is thrown.
Changes to a serializable class can be compatible or incompatible. Following is the list of changes which are compatible:
•Add fields
•Change a field from static to non-static
•Change a field from transient to non-transient
•Add classes to the object tree
List of incompatible changes:
•Delete fields
•Change class hierarchy
•Change non-static to static
•Change non-transient to transient
•Change type of a primitive field
So, if no suid is present , inspite of making compatible changes, jvm generates new suid thus resulting in an exception if prior release version object is used .
The only way to get rid of the exception is to recompile and deploy the application again.
If we explicitly metion the suid using the statement:
private final static long serialVersionUID =
then if any of the metioned compatible changes are made the class need not to be recompiled. But for incompatible changes there is no other way than to compile again.

Java Collections API Interview Questions

1. You need to insert huge amount of objects and randomly delete them one by one. Which Collection data structure is best pet?
2. What goes wrong if the HashMap key has same hashCode value?
3. If hashCode() method is overridden but equals() is not, for the class ‘A’, then what may go wrong if you use this class as a key in HashMap?
4. How will you remove duplicate element from a List?
5. How will you synchronize a Collection class dynamically?
ANSWERS:

1. Obviously, LinkedList.
2. It leads to ‘Collision’ wherein all the values are stored in same bucket. Hence, the searching time increases quad radically.
3. Left to Readers.
4. Add the List elements to Set. Duplicates will be removed.
5. Use the utility method 
REference :http://excusemeworld.com/java-j2ee-interview-questions/java-collections-api-interview-questions/