Question-171. When should we interrupt a thread?
Answer- We should interrupt a thread if we want to break out the sleep or wait state of a thread.
Question-172. What is synchronization ?
Answer- Synchronization is the capabilility of control the access of multiple threads to any shared resource.It is used:
To prevent thread interference. To prevent consistency problem.
Question-173. What is the purpose of Synchronized block ?
Answer- Synchronized block is used to lock an object for any shared resource.
Scope of synchronized block is smaller than the method.
Question-174. Can Java object be locked down for exclusive use by a given thread ?
Answer- Yes. You can lock an object by putting it in a “synchronized” block. The locked object is inaccessible to any thread other than the one that explicitly claimed it.
Question-175. What is static synchronization ?
Answer- If you make any static method as synchronized, the lock will be on the class not on object.
Question-176. What is the difference between notify() and notifyAll() ?
Answer- The notify() is used to unblock one waiting thread whereas notifyAll() method is used to unblock all the threads in waiting state.
Question-177. What is deadlock ?
Answer- Deadlock is a situation when two threads are waiting on each other to release a resource. Each thread waiting for a resource which is held by the other waiting thread.
Question-178. What is the difference between ArrayList and Vector ?
Answer-
ArrayList | Vector |
---|---|
1) ArrayList is not synchronized. | 1) Vector is synchronized. |
2) ArrayList is not a legacy class. | 2) Vector is a legacy class. |
3) ArrayList increases its size by 50% of the array size. | 3) Vector increases its size by doubling the array size. |
Question-179. What is the difference between ArrayList and LinkedList ?
Answer-
ArrayList | LinkedList |
---|---|
1) ArrayList uses a dynamic array. | 1) LinkedList uses doubly linked list. |
2) ArrayList is not efficient for manipulation because a lot of shifting is required. | 2) LinkedList is efficient for manipulation. |
3) ArrayList is better to store and fetch data. | 3) LinkedList is better to manipulate data. |
Question-180. What is the difference between Iterator and ListIterator ?
Answer- Iterator traverses the elements in forward direction only whereas ListIterator traverses the elements in forward and backward direction.
Iterator | ListIterator |
---|---|
1) Iterator traverses the elements in forward direction only. | 1) ListIterator traverses the elements in backward and forward directions both. |
2) Iterator can be used in List, Set and Queue. | 2) ListIterator can be used in List only. |