2 Replies Latest reply: Jul 18, 2008 7:25 AM by 807589 RSS

    proof on arraylist and vector synchronize property

    807589
      I tryed to do a sample program which shows the difference between array list and vector on there synchronize difference.

      I dont know am i in the right way of learning it.

      I have created a object for a class named Servlet
      then
      I have created 2 thread named "Request A" and "Request B"
      and passed the object reference to those thread.

      i like to know difference between array list and vector using code

      please help

      thanks in advance
      import java.util.ArrayList;
      import java.util.List;
      import java.util.Vector;
      
      public class VecArr {
      
           public static void main(String[] args) throws InterruptedException {
      
                Servlet s = new Servlet();
      
                new Thread(new MyThread("Request A ", s)).start();
                new Thread(new MyThread("Request B ", s)).start();
      
           }
      }
      
      class Servlet {
      
           //private List v = new Vector();
           private List v = new ArrayList();
      
           public List getV() {
      
                return v;
           }
      
           void doGet(String data) {
      
                System.out.println(data);
                v.add(data);
      
           }
      
      }
      
      class MyThread implements Runnable {
      
           String name;
      
           Servlet s;
      
           MyThread(String name, Servlet s) {
                this.name = name;
                this.s = s;
           }
      
           public void run() {
      
                s.doGet("Name : " + name );
      
           }
      
      }
        • 1. Re: proof on arraylist and vector synchronize property
          807589
          package demo;
          
          import java.util.List;
          
          public interface MyRunArr extends List {
          
               public boolean add(Object arg0, String name);
          }
          import java.util.AbstractCollection;
          import java.util.AbstractList;
          import java.util.ArrayList;
          
          
          
          public class RunTimeArrAL extends ArrayList implements MyRunArr {
          
               public boolean add(Object arg0, String name) {
          
                    try {
                         System.out.println("name : " + name + " waiting...");
                         Thread.currentThread().sleep(1000 * 5);
                    } catch (InterruptedException e) {
                         e.printStackTrace();
                    }
                    return super.add(arg0);
               }
          }
          public class RunTimeArrVec extends Vector implements MyRunArr {
          
               public synchronized boolean add(Object arg0, String name) {
          
                    try {
                         System.out.println("name : " + name + " waiting...");
                         Thread.currentThread().sleep(1000 * 5);
                    } catch (InterruptedException e) {
                         e.printStackTrace();
                    }
                    return super.add(arg0);
               }
          }
          import java.util.ArrayList;
          import java.util.Vector;
          
          public class RunTimeArrTest   {
          
               public static void main(String[] args) {
          
                    MyRunArr rAr = new RunTimeArrVec();
          
                    new Thread(new RunTimeService(rAr,"Thread A",2)).start();
                    new Thread(new RunTimeService(rAr,"Thread B",1)).start();
          
               }
          }
          
          class RunTimeService implements Runnable {
          
               private MyRunArr rArr;
               
               private String name;
               
               private int count;
          
               public RunTimeService(MyRunArr rArr, String name, int count) {
                    this.rArr = rArr;
                    this.name=name;
                    this.count=count;
               }
          
               public void run() {
          
                    for (int i=0;i<count;i++) {
                         //System.out.println("name : "+name+" "+i);
                         //System.out.println("name : "+name+" adding...");
                         rArr.add("a",name);               
                    }
               }
          }
          • 2. Re: proof on arraylist and vector synchronize property
            807589
            i have undestood .

            please refere the sample program to know better