This discussion is archived
1 Reply Latest reply: Aug 27, 2008 9:50 AM by 3004 RSS

Need help in Understanding synchronization through code

581508 Newbie
Currently Being Moderated
I read someone that synchronization is suppose to allow only one thread to access a synchronized method or block of code. But then I wrote some code just to test it out.
import java.util.ArrayList;
import java.util.Vector;

public class Testing extends Thread {

     private String someString;

     static Vector<Integer> someVec;

     static ArrayList<Integer> someArrayList;

     public Testing(String value) {

          someString = value;
          someVec = new Vector<Integer>();
          someArrayList = new ArrayList<Integer>();
     }

     public static void main(String args[]) {

          Testing one = new Testing("one");
          Testing two = new Testing("two");
          one.start();
          two.start();
     }

     public void run() {

          if (someString.equals("one")) {
               for (int i = 0; i < 1000; i++) {
                    try {
                         someVec.add(i);
                    } catch (RuntimeException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                    }
                    System.out.println("adding with " + i);
               }
          } else
               for (int i = 0; i < someVec.size(); i++)
                    try {
                         System.out.println("getting .. " + someVec.get(i)+ " from "+someVec.size());
                    } catch (RuntimeException e) {
                         // TODO Auto-generated catch block
                         e.printStackTrace();
                    }
     }
}
Now my questions

1. Why the 2nd thread is able to access the someVec ( Vector) when the 1st thread is still adding some values?
2. When I changed someVec to someArrayList the result is still the same. I wonder why ?
3. Lastly, Can anyone modify my code so only either a Vector or a Arraylist will make the program run without error ? Because right now I can run them both fine, so I don't see the difference.

Thank you.

Regards,

Mustafa
  • 1. Re: Need help in Understanding synchronization through code
    3004 Newbie
    Currently Being Moderated
    Vrijmetse wrote:
    1. Why the 2nd thread is able to access the someVec ( Vector) when the 1st thread is still adding some values?
    You have not added any synchronization around your Vector operations. Vector's individual methods are synchronized, but that only extends to the execution of those methods. There's no way for that syncing to know that you're doing stuff in a for loop. If you want to complete the for loop without anything else affecting that Vector, then you need a sync block around the body of the loop, and anything else that acceses the Vector must also sync on the same object. The Vector itself is a good candidate for the lock here.

    (There may be other problems as well. I haven't read your code closely. Just noticed the for loop and lack of syncing.)
    2. When I changed someVec to someArrayList the result is still the same. I wonder why ?
    Same reason.
    3. Lastly, Can anyone modify my code so only either a Vector or a Arraylist will make the program run without error ?
    No.