Saturday, 19 October 2013

ArrayList vs Vector


ArrayList vs Vector

ArrayList vs Vector in Java
ArrayList and Vector is two most widely used Collection class in Java and used to store object in ordered fashion. Every Java programmer which is introduced to Java Collection Framework either started with Vector or ArrayList. For beginners Difference between Vector and ArrayList in Java  and 
LinkedList vs ArrayList are two most  popular Java Interview question. ArrayList vs Vector is not only important on interview perspective but also on effective use of Java Collection API. After reading this article you will know when to use Vector in Java, When to use ArrayList in Java and would be able to compare ArrayList vs Vector over several important parameters e.g. Speed, Synchronization, Code quality etc. Before seeingdifference on Vector vs ArrayList, let's What is common between the.


Common property of Vector and ArrayList in Java


1) Bother Vector and ArrayList are derived from AbstractList and implements 
List interface, which means both of them are ordered collection and allows duplicates.

2) Another similarity between Vector vs ArrayList is that both are index based Collection and you can use get(index)method to retrieve objects from 
Vector and ArrayList.



Vector vs ArrayList in Java

In last section we saw some common properties between both of them and its time to see How muchArrayList and Vector are different to each other.

1) First and most common difference between Vector vs ArrayList is that Vector is 
synchronized and thread-safewhile ArrayList is neither Synchronized nor thread-safe. Now, What does that mean? It means if multiple thread try to access Vector same time they can do that without compromising Vector's internal state. Same is not true in case ofArrayList as methods like add(), remove() or get() is not synchronized.

2) Second major difference on Vector vs ArrayList is Speed, which is directly related to previous difference. SinceVector is synchronized, its slow and 
ArrayList is not synchronized its faster than Vector.

3) Third difference on Vector vs ArrayList is that Vector is a legacy class and initially it was not part of 
Java Collection Framework. From Java 1.4 Vector was retrofitted to implement List interface and become part of Collection Framework.

These were some comparison on Vector vs ArrayList. In Summary use ArrayList if you are using ArrayList in Single threaded environment and use Vector if you need a thread-safe collection. ArrayList is anytime faster than Vector in case thread-safety is not a concern.

wait and sleep methods description

What are difference between wait and sleep in Java


Wait vs sleep in Java
Differences between wait and sleep method in Java Thread is one of the very old question asked in Java interviews. Though both wait and sleep puts thread on waiting state, they are completely different in terms of behaviour and use cases. Sleep is meant forintroducing pause, releasing CPU and giving another thread opportunity to execute while wait is used for inter thread communication, by using wait() and notify() method two threads can communicate with each other which is key to solve many Concurrent problems like Produce consumer issue, Dining philosopher issue and to implement several Concurrency designs. In this tutorial we will see

·                             What is wait method in Java
·                             What is Sleep method in Java
·                             Difference between wait and sleep in Java
·                             Where to use wait and sleep in Java

What is wait and sleep method in Java

Wait method is defined in Object class and it available to all object, wait()method is always discussed along with its counterpart notify() andnotifyAll() method and used in inter thread communication in Java. wait method puts a thread on wait by checking some condition like in Producer Consumer problem, producer thread should wait if Queue is full or Consumer thread should wait if Queue is empty. notify() method is used to wake up waiting thread by communicating that waiting condition is over now for example once producer thread puts an item on empty queue it can notify Consumer thread that Queue is not empty any more. On the other hand Sleep() method is used to introduce pause on Java application. You can put a Thread on sleep, where it does not do anything and relinquish the CPU for specified duration. When a Thread goes to Sleep it can be either wake up normally after sleep duration elapsed or it can be woken up abnormally by interrupting it.

Difference between Wait and Sleep method in Java Thread

In last section we saw what is wait and sleep method and in this section we will see what are differences between wait and sleep method in Java. As I told before apart from waiting they are completely different to each other:

1) First and most important difference between Wait and sleep method is that wait method must be called from synchronized context i.e. from synchronized method or block in Java. If you call wait method without synchronization, it will throwIllegalMonitorStateException in Java. On the other hand there is no requirement of synchronization for calling sleep method , you can call it normally.

2) Second worth noting difference between wait and sleep method is that, wait operates on Object and defined in Object class while sleep operates on current Thread and defined in java.lang.Thread class.

3) Third and another significant difference between wait and sleep in Java is that, wait() method releases the lock of object on which it has called, it does release other locks if it holds any while sleep method of Thread class does not release any lock at all.

4) wait method needs to be called from a loop in order to deal with false alarm i.e. waking even though waiting condition still holds true, while there is no such thing for sleep method in Java. its better not to call Sleep method from loop.

here is code snippet for calling wait and sleep method in Java

synchronized(monitor)
while(condition == true){ monitor.wait())  //releases monitor lock

Thread.
sleep(100); //puts current thread on Sleep


5) One more difference between wait and sleep method which is not as significant as previous ones is that wait() is a non static method while sleep() is static method in Java.

Where to use wait and sleep method in Java

By reading properties and behavior of wait and sleep method it's clear that wait() method should be used in conjunction withnotify() or notifyAll() method and intended for communication between two threads in Java while Thread.sleep() method is a utility method to introduce short pauses during program or thread execution. Given the requirement of synchronization for wait, it should not be used just to introduce pause or sleep in Java.