Stranger
Stranger

Reputation: 862

Examples of Vector vs ArrayList Synchronization

Vectors contain synchronized methods, and ArrayLists do not. These synchronized methods help prevent data corruption when the data is accessed and modified by >1 thread.

Can someone explain this in more detail for me? What does it mean that Vectors contain synchronized methods? Do the methods contain internal locks that control the locking and unlocking of the data for multiple thread access? Can someone provide some examples of where using a vector (vs. ArrayList) could prevent data corruption and shed some more light on the issue or Data Structures and synchronization issues?

Thanks for your time and help .

Upvotes: 2

Views: 2203

Answers (2)

sweetpoision
sweetpoision

Reputation: 77

Haha. 8 years old question. I was searching for the answer but then created a Demo myself by learning from online sources. Here's what I understand: You can use synchronized keyword on a method of the Object you want to be threadsafe, or you can write synchronized objName wherever you're going to use that object. Here's an example, in case it helps anyone. Here the SyncData is the Class of which I created synchronized object (data).

class SyncData {
    synchronized public void print() {
//      public void print() {
        for(int i=1; i<=5; i++) {
            System.out.println("SyncData: print() : "  + Thread.currentThread().getName() + " => " + i);
            try {
                Thread.sleep(1000);
            } catch(InterruptedException excep) {
                System.out.println(excep.getMessage());
            }
        }
    }
}

public class SynchronizedDemo {

    static class SyncChildThread extends Thread {
        SyncData data;
        public SyncChildThread(SyncData data) {
            this.data = data;
        }
        @Override
        public void run() {     
//          synchronized(data) { //alternative way
//              data.print();
//          }
            data.print();
        }
    }

    public static void main(String[] args) {
        //dummy data, which will be shared among multiple threads
        SyncData data = new SyncData();
        SyncChildThread threadOne, threadTwo;
        threadOne = new SyncChildThread(data);
        threadOne.setName("Thread-1");
        threadTwo = new SyncChildThread(data);
        threadTwo.setName("Thread-2");
        threadOne.start();
        threadTwo.start();
    }
}

The synchronized output will be :

SyncData: print() : Thread-1 => 1
SyncData: print() : Thread-1 => 2
SyncData: print() : Thread-1 => 3
SyncData: print() : Thread-1 => 4
SyncData: print() : Thread-1 => 5
SyncData: print() : Thread-2 => 1
SyncData: print() : Thread-2 => 2
SyncData: print() : Thread-2 => 3
SyncData: print() : Thread-2 => 4
SyncData: print() : Thread-2 => 5

If you remove synchronized, then any thread can access the object anytime(simultaneous access). The output of non-synchronized access of object data:

 AsyncData: print() : Thread-1 => 1
AsyncData: print() : Thread-2 => 1
AsyncData: print() : Thread-1 => 2
AsyncData: print() : Thread-2 => 2
AsyncData: print() : Thread-1 => 3
AsyncData: print() : Thread-2 => 3
AsyncData: print() : Thread-1 => 4
AsyncData: print() : Thread-2 => 4
AsyncData: print() : Thread-1 => 5
AsyncData: print() : Thread-2 => 5

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533660

What does it mean that Vectors contain synchronized methods?

The methods have been marked as synchronized.

Do the methods contain internal locks that control the locking and unlocking of the data for multiple thread access?

No, despite the keyword being on the method, it is the object which is locked, not the method. There is no way to lock a method as such.

Can someone provide some examples of where using a vector (vs. ArrayList) could prevent data corruption and shed some more light on the issue or Data Structures and synchronization issues?

There are many examples. The simplest is incrementing a number. Say you have two threads incrementing a number (e.g. the size of the collection) Without synchronization you can have.

Thread1: read size e.g. 0
Thread1: increment size
Thread1: write size e.g. 1
Thread2: read size e.g. 1
Thread2: increment size
Thread2: write size e.g. 2

however as threads can perform actions in any order (as this is the whole point of threads) you can also have

Thread1: read size e.g. 0
Thread2: read size e.g. 0
Thread1: increment size
Thread2: increment size
Thread1: write size e.g. 1
Thread2: write size e.g. 1

So even though two threads have incremented the counter, it is incorrect because their actions where not co-ordinated. This is what synchronization does for you.

Upvotes: 3

Related Questions