Vector is thread-safe, is not in multi-threaded operation vector can not add synchronized

Source: Internet
Author: User

As in the title, if I were to answer before, I would say, yes, in the multi-threaded environment to operate the vector, do not need to add synchronized.

But I accidentally saw an article today, I found that my previous idea was wrong, the address of this article:

http://zhangbq168.blog.163.com/blog/static/2373530520082332459511/

I extract the key part:

Vectors are slower than ArrayList because the vectors themselves are synchronized, and ArrayList is not
Therefore, no synchronization is involved with the recommended ArrayList.

See the 3rd paragraph of the JDK about vector description:
As of the Java 2 Platform v1.2, this class had been retrofitted to implement List, so that's it becomes a part of Java ' s COL Lection Framework. Unlike the new collection implementations, Vector is synchronized.

Obviously, the vector is synchronous, landlord if you do not want to achieve synchronization, or will use the vector
Since everyone has talked about synchronization, then also a little talk about, synchronized Hashtable and vector really so useful? Really if you use the socalled thread-safe Hashtable and Vector program code will not need to sync it?
This example (Vector VEC; Object element;)
if (!vec.contains (Element))
Vec.add (Element);
Can this code be out of sync? No, context switch might take place right after your do the containg check.
Therefore, in the program still need:
Synchronized (VEC)
{
if (!vec.contains (Element))
Vec.add (Element);
}
This synchronized vector has no advantage over synchronized ArrayList and LinkedList.

When I saw the content, I found that, yes, the current vector is the existence of this element, if not, add it.

I looked at the Vector.java source code and found that the contains method is not synchronous.

I have also written a test program, and the results prove that in the program still need:
synchronized (VEC)
{
if (!vec.contains (Element))
Vec.add (Element);
}

--------------------------------------------------------------------------------------------------------------- ------

The test code I wrote is as follows:

/*
* Test vector in multi-threaded operation does not really need to add Synchronized
*/
Package test.thread;

Import Java.util.Vector;

public class TestVector2
{
Vector<integer> data;

Public TestVector2 ()
{
data = new vector<integer> ();

for (int i=0;i<10;i++)
{
New Add (). Start ();
}

New Del (). Start ();
}

Add in Multithreading
Class Add extends Thread
{
public void Run ()
{
for (int i=0;i<100;i++)
{
Synchronized (data) {
if (!data.contains (6))
{
Data.add (6);

int size = Data.size ();

if (Size > 1)
{
System.out.println ("data.size > 1 =" + size);

for (int v:data)
{
System.err.println (v);
}
//        }

}
}


Try
{
Sleep ((int) (Math.random () * 30));
}
catch (Interruptedexception e)
{
E.printstacktrace ();
}
}
}
}

Delete
Class Del extends Thread
{
public void Run ()
{
for (int i=0;i<100;i++)
{
if (Data.contains (6))
{
Data.removeelement (6);
}

Try
{
Sleep ((int) (Math.random () * 30));
}
catch (Interruptedexception e)
{
E.printstacktrace ();
}
}
}
}

public static void Main (string[] args)
{
New TestVector2 ();
}

}

Result screenshot:

2012-02-28

Vector is thread-safe, is not in multi-threaded operation vector can not add synchronized

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.