Advantage of using Vector in multithreading environment in java


In this Collection framework tutorial we will discuss advantage of using Vector in multithreading environment in java with program and examples. In previous tutorial we learned consequence of using ArrayList in multithreading environment in java.



Create Thread-1 and Thread-2, then
Start Thread-1 and Thread-2 (ensure Thread-2 starts after Thread-1)

Thread-1 will add element in Vector (by default element will be added on 0th index in empty Vector), let’s say thread takes some time in adding element to vector (by using Thread.sleep(1000)).
As soon as Thread-1 will call add method it will acquire lock on Vector object.

MeanWhile, Thread-2 will try to get element on 0th index of vector and wait for add method to release lock on vector object. Hence, get method will return element on 0th index without throwing any exception (In previous post we saw consequence of using ArrayList in multithreading environment in java).

Example/Program to show advantage of using Vector in multithreading environment in java >
package com.ankit;
import java.util.Arrays;
/**
* @author AnkitMittal, javaMadeSoeasy.com
* Copyright (c), AnkitMittal . All Contents are copyrighted and
* must not be reproduced in any form.
* This class provides custom implementation of Vector(without using java api's)
* Insertion order of objects is maintained.
* Implementation allows you to store null as well.
* @param <E>
*/
class VectorCustom<E> {
  
 private static final int INITIAL_CAPACITY = 10;
 private Object elementData[]={};
 private int size = 0;
 /**
 * constructor.
 */
 public VectorCustom() {
   elementData = new Object[INITIAL_CAPACITY];
 }
 /**
  * method adds elements in VectorCustom.
  */
 public synchronized void add(E e) {
   if (size == elementData.length) {
     ensureCapacity(); //increase current capacity of list, make it double.
   }
  
   // Let's say current thread is taking some time in adding element to list.
   try {
          Thread.sleep(1000);
   } catch (InterruptedException ex) {
          ex.printStackTrace();
   }
   elementData[size++] = e;
 }
 /**
  * method returns element on specific index.
  */
 @SuppressWarnings("unchecked")
 public synchronized E get(int index) {
   //if index is negative or greater than size of size, we throw Exception.
   if ( index <0 || index>= size) {
     throw new IndexOutOfBoundsException("Index: " + index + ", Size " + index);
   }
   return (E) elementData[index]; //return value on index.
 }
 /**
  * method returns removedElement on specific index.
  * else it throws IndexOutOfBoundException if index
  * is negative or greater than size of size.
  */
 public synchronized Object remove(int index) {
   //if index is negative or greater than size of size, we throw Exception.
   if ( index <0 || index>= size) {
     throw new IndexOutOfBoundsException("Index: " + index + ", Size " + index);
   }
  
   Object removedElement=elementData[index];
   for(int i=index;i<size;i++){
      elementData[i]=elementData[i+1];
   }
   size--;   //reduce size of VectorCustom after removal of element.
  
   return removedElement;
 }
 /**
  * method increases capacity of list by making it double.
  */
 private void ensureCapacity() {
   int newIncreasedCapacity = elementData.length * 2;
   elementData = Arrays.copyOf(elementData, newIncreasedCapacity);
 }
 /**
  * method displays all the elements in list.
  */
 public void display() {
    System.out.print("Displaying list : ");
    for(int i=0;i<size;i++){
           System.out.print(elementData[i]+" ");
    }
 }
}
/**
* Main class to test VectorCustom functionality.
*/
public class VectorCustomExample {
  
    public static void main(String...a) throws InterruptedException {
                 final VectorCustom<Integer> list = new VectorCustom<Integer>();
      //Thread-1 will add element in list.
                 new Thread() {
                       public void run() {
                              list.add(54);
                       }
                 }.start();
                 //Below sleep ensures Thread-2 starts after Thread-1
                 Thread.sleep(10);
                
                 //Thread-2 will get element from list.
                 new Thread() {
                       public void run() {
                              System.out.println("element at index " + 0 + " = "
                                                   + list.get(0));
                       }
                 }.start();
          }
  
}
/*Output
element at index 0 = 54
*/


In this Collection framework tutorial we discussed advantage of using Vector in multithreading environment in java with program and examples.

Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking on facebook, following on google+ or Twitter.


RELATED LINKS>

Consequence of using ArrayList in multithreading environment in java



No comments:

Post a Comment