Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
2) Synchronized block provide granular control over lock, as you can use
arbitrary any lock to provide mutual exclusion to critical section code. On the
other hand synchronized method always lock either on current object
represented by this keyword or class level lock, if its static synchronized
method.
1. Static method : Both yield and sleep method are static method . Hence ,
they always change the state of currently executing thread .
1. Class belongs : The wait() method belongs to java.lang.Object class, thus can
be called on any Object. The sleep() method belongs to java.lang.Thread class, thus
can be called on Threads.
2. Context : The wait() method can only be called from Synchronized context i.e.
using synchronized block or synchronized method. The sleep() method can be called
from any context.
3. Locking : The wait() method releases the lock on an object and gives others
chance to execute. The sleep() method does not releases the lock of an object for
specified time or until interrupt.
Thread state: Both the method wait() and sleep() makes the running thread into
Not Runnable state.
Running time: Both the method wait() and sleep() takes total execution time in
milliseconds as an argument, after that it will be expired.
Exception
IllegalArgumentException if the value of timeout is negative.
IllegalMonitorStateException if the current thread is not the
owner of the object's monitor.
InterruptedException if another thread has interrupted the current
thread. The interrupted status of the current thread is cleared when
this exception is thrown.
When to use wait()
The wait() is used for time synchronization, where the thread actually
needs a delay in background.
For example, process something on specific interval.
What must be the order of catch blocks when catching more than
one exception?
The sub classes must come first. Otherwise it will give a compile time error.
Whats HQL ?
HQL is the query language used in Hibernate which is an extension of SQL.
HQL is very efficient, simple and flexible query language to do various type
of operations on relational database without writing complex database
queries.
HashMap object uses Key object hashCode() method and equals() method to
find out the index to put the key-value pair. If we want to get value from the
HashMap same both methods are used . Somehow, if both methods are not
implemented correctly , it will result in two keys producing the same
hashCode() and equals() output. The problem will arise that HashMap will
treat both output same instead of different and overwrite the most recent
key-value pair with the previous key-value pair.
Similarly all the collection classes that does not allow the duplicate values
use hashCode() and equals() method to find the duplicate elements.So it is
very important to implement them correctly.
Ans. "equals" is the member of object class which returns true if the content
of objects are same whereas "==" evaluate to see if the object handlers on
the left and right are pointing to the same object in memory.
1) use == to compare primitive e.g. boolean, int, char etc, while use equals()
to compare objects in Java.
2) == return true if two reference are of same object. Result of equals()
method depends on overridden implementation.
3) For comparing String use equals() instead of == equality operator.
Explain java.lang.OutOfMemoryError ?
Ans. This Error is thrown when the Java Virtual Machine cannot allocate an
object because it is out of memory, and no more memory could be made
available by the garbage collector.
Ans. Finalize is used by Java for Garbage collection. It should not be done as
we should leave the Garbage Collection to Java itself.