1. import java.util.ArrayList;
  2. import java.util.List;
  3. //实现Runnable接口的线程
  4. public class HelloThread implements Runnable {
  5. String name;
  6. List<String> v;
  7. HelloThread(String name, List<String> v) {
  8. this.name = name;
  9. this.v = v;
  10. }
  11. public void run() {
  12. System.out.println(name + "start");
  13. while(true) {
  14. v.add(name + ".add");
  15. System.out.println(name + " list size is " + v.size());
  16. try {
  17. Thread.sleep(10);
  18. } catch(InterruptedException e) {
  19. System.out.println(e.getMessage());
  20. }
  21. }
  22. }
  23. public static void main(String args[]) throws InterruptedException {
  24. List<String> v = new ArrayList<String>();
  25. HelloThread hello1 = new HelloThread("hello1", v);
  26. HelloThread hello2 = new HelloThread("hello2", v);
  27. HelloThread hello3 = new HelloThread("hello3", v);
  28. Thread h1 = new Thread(hello1);
  29. Thread h2 = new Thread(hello2);
  30. Thread h3 = new Thread(hello3);
  31. h1.start();
  32. h2.start();
  33. h3.start();
  34. }
  35. }

结果:

hello3start
hello3 list size is 1
hello1start
hello1 list size is 2
hello2start
hello2 list size is 3
hello3 list size is 4 
hello1 list size is 5
hello2 list size is 4 
hello3 list size is 6
hello1 list size is 8
hello2 list size is 7
hello1 list size is 9 
hello3 list size is 10
hello2 list size is 9

加了12次,但size却只有10,不安全

改成号称线程安全的Vector:

  1. import java.util.Vector;
  2. //实现Runnable接口的线程
  3. public class HelloThread implements Runnable {
  4. String name;
  5. Vector<String> v;
  6. HelloThread(String name, Vector<String> v) {
  7. this.name = name;
  8. this.v = v;
  9. }
  10. public void run() {
  11. System.out.println(name + "start");
  12. while(true) {
  13. v.add(name + ".add");
  14. System.out.println(name + " vector size is " + v.size());
  15. try {
  16. Thread.sleep(10);
  17. } catch(InterruptedException e) {
  18. System.out.println(e.getMessage());
  19. }
  20. }
  21. }
  22. public static void main(String args[]) throws InterruptedException {
  23. Vector<String> v = new Vector<String>();
  24. HelloThread hello1 = new HelloThread("hello1", v);
  25. HelloThread hello2 = new HelloThread("hello2", v);
  26. HelloThread hello3 = new HelloThread("hello3", v);
  27. Thread h1 = new Thread(hello1);
  28. Thread h2 = new Thread(hello2);
  29. Thread h3 = new Thread(hello3);
  30. h1.start();
  31. h2.start();
  32. h3.start();
  33. }
  34. }

结果:

hello1start
hello1 vector size is 1
hello2start
hello2 vector size is 2
hello3start
hello3 vector size is 3
hello1 vector size is 4
hello2 vector size is 5
hello3 vector size is 6
hello1 vector size is 7
hello2 vector size is 8
hello3 vector size is 9
hello1 vector size is 10
hello2 vector size is 11
hello3 vector size is 12
hello1 vector size is 13
hello3 vector size is 15
hello2 vector size is 15 
hello1 vector size is 16

也出现了线程不安全现象吗?不是的

这个不算线程不安全,加了16次,size是16,恰恰是线程安全的表现,只不过是待两个线程都add完了之后才调的size(),所以都是15,跳过了14。

以上一样的程序多试几次就出现了,另外关于Vector的thread-safety

All Vector methods are synchronized themselves, so as long as you are only synchronizing around a single method, your own synchronization is not necessary. If you have several method calls, which depend on each other, e.g. something like vec.get(vec.size()-2) to get the second last element, you have to use your own synchronization since otherwise, the vector may change between vec.size() and vec.get().

所有的Vector的方法对它们自己而言都是 synchronized的,所以如果只要同步单个方法,自己额外添加的同步措施就失去必要了。如果有几个方法需要调用,且它们互相存在依赖,比如 vec.get(vec.size()-2),是要得到倒数第二个元素,那么就必须加上自己的同步措施,因为否则的话,vector有可能在 vec.size() 和 vec.get() 之间发生改变

ArrayList,Vector线程安全性测试的更多相关文章

  1. ArrayList的线程安全测试

    public class TestThread implements Runnable{ private List list; CountDownLatch cdl; public TestThrea ...

  2. Spring中获取request的几种方法,及其线程安全性分析

    前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...

  3. [No000016E]Spring 中获取 request 的几种方法,及其线程安全性分析

    前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...

  4. Spring中获取request的几种方法,及其线程安全性分析(山东数漫江湖)

    前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...

  5. Spring中如何获取request的方法汇总及其线程安全性分析

    前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性.下面话不多说了,来一起看看详细的介绍吧. 概述 在使用Spring MVC开发Web系统 ...

  6. ConcurrentHashMap和 CopyOnWriteArrayList提供线程安全性和可伸缩性 以及 同步的集合类 Hashtable 和 Vector Collections.synchronizedMap 和 Collections.synchronizedList 区别缺点

    ConcurrentHashMap和 CopyOnWriteArrayList提供线程安全性和可伸缩性 DougLea的 util.concurrent 包除了包含许多其他有用的并发构造块之外,还包含 ...

  7. 我是怎样测试Java类的线程安全性的

    线程安全性是Java等语言/平台中类的一个重要标准,在Java中,我们经常在线程之间共享对象.由于缺乏线程安全性而导致的问题很难调试,因为它们是偶发的,而且几乎不可能有目的地重现.如何测试对象以确保它 ...

  8. 分享和探讨——如何测试Java类的线程安全性?

    缺乏线程安全性导致的问题很难调试,因为它们是零星的,几乎不可能有意复制.你如何测试对象以确保它们是线程安全的? 我在最近的学习中和优锐课老师谈到了这个问题.现在,是时候以书面形式进行解释了.线程安全是 ...

  9. Vector线程安全,ArrayList非线程安全

    http://baijiahao.baidu.com/s?id=1638844080997170869&wfr=spider&for=pc Vector线程安全,ArrayList非线 ...

随机推荐

  1. 拉动滚动条追加内容,无限延伸document高度 $(window).scroll(function(){if($(window).scrollTop() + $(window).height() == $(document).height()) { $("body").append(html) } })

    $(document).ready(function() { // endless scrolling $(window).scroll(function() { if($(window).scrol ...

  2. 网络-CIDR地址分类介绍

    CIDR(Classless Inter Domain Routing)改进了传统的IPv4地址分类.传统的IP分类将IP地址直接对应为默认的分类,从而将Internet分割为网络.CIDR在路由表中 ...

  3. Linux-IP地址后边加个/8(16,24,32)是什么意思?

    是掩码的位数        A类IP地址的默认子网掩码为255.0.0.0(由于255相当于二进制的8位1,所以也缩写成“/8”,表示网络号占了8位);    B类的为255.255.0.0(/16) ...

  4. share point 读取 List数据

    SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite oSite = new SPSite(siteUrl)) { forea ...

  5. PHP GC垃圾回收机制之引用变量回收周期疑问

    普通的引用变量的销毁大家都知道, 当unset的时候如果refcount = 0 则认为无用, 销毁. 但是手册中提到一点会有递归引用的问题,很是奇葩 代码如下 <?php $a = 1; $a ...

  6. Onload,Onunload,onbeforeunload,$(window).load(function() {})和$(document).ready(function(){})

    Onload,$(window).load(function() {}):元素都加载完毕,才可以执行. $(document).ready(function(){}):不一定要等所有的js和图片加载完 ...

  7. 虚拟化之vmware虚拟机扩容与克隆

    前提条件: 1.在新建虚拟机的时候创建磁盘如下图,之后这个磁盘就可以在虚拟机关机状态下动态扩展大小,否则只能通过添加另外一块硬盘的方式来扩容.

  8. typeof和instanceof运算符

    (1)typeof运算符用于判断某一个变量的数据类型,它可以作为函数来用,如typeof(a)可以返回变量a的数据类型:也可以作为一个运算符来使用,例如typeof a 也可以返回变量a的数据类型,不 ...

  9. 百度ueditor学习使用

    1.下载ueditor解压 2. jsp里存放着开发所用到的jar包,导入项目即可 前端页面代码如下: <center> <form action="" meth ...

  10. mysql 进程状态(转)

    通过show processlist查看MySQL的进程状态,在State列上面的状态有如下这些: Analyzing线程对MyISAM 表的统计信息做分析(例如, ANALYZE TABLE ).c ...