ArrayList,Vector线程安全性测试
- import java.util.ArrayList;
- import java.util.List;
- //实现Runnable接口的线程
- public class HelloThread implements Runnable {
- String name;
- List<String> v;
- HelloThread(String name, List<String> v) {
- this.name = name;
- this.v = v;
- }
- public void run() {
- System.out.println(name + "start");
- while(true) {
- v.add(name + ".add");
- System.out.println(name + " list size is " + v.size());
- try {
- Thread.sleep(10);
- } catch(InterruptedException e) {
- System.out.println(e.getMessage());
- }
- }
- }
- public static void main(String args[]) throws InterruptedException {
- List<String> v = new ArrayList<String>();
- HelloThread hello1 = new HelloThread("hello1", v);
- HelloThread hello2 = new HelloThread("hello2", v);
- HelloThread hello3 = new HelloThread("hello3", v);
- Thread h1 = new Thread(hello1);
- Thread h2 = new Thread(hello2);
- Thread h3 = new Thread(hello3);
- h1.start();
- h2.start();
- h3.start();
- }
- }
结果:
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:
- import java.util.Vector;
- //实现Runnable接口的线程
- public class HelloThread implements Runnable {
- String name;
- Vector<String> v;
- HelloThread(String name, Vector<String> v) {
- this.name = name;
- this.v = v;
- }
- public void run() {
- System.out.println(name + "start");
- while(true) {
- v.add(name + ".add");
- System.out.println(name + " vector size is " + v.size());
- try {
- Thread.sleep(10);
- } catch(InterruptedException e) {
- System.out.println(e.getMessage());
- }
- }
- }
- public static void main(String args[]) throws InterruptedException {
- Vector<String> v = new Vector<String>();
- HelloThread hello1 = new HelloThread("hello1", v);
- HelloThread hello2 = new HelloThread("hello2", v);
- HelloThread hello3 = new HelloThread("hello3", v);
- Thread h1 = new Thread(hello1);
- Thread h2 = new Thread(hello2);
- Thread h3 = new Thread(hello3);
- h1.start();
- h2.start();
- h3.start();
- }
- }
结果:
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线程安全性测试的更多相关文章
- ArrayList的线程安全测试
public class TestThread implements Runnable{ private List list; CountDownLatch cdl; public TestThrea ...
- Spring中获取request的几种方法,及其线程安全性分析
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...
- [No000016E]Spring 中获取 request 的几种方法,及其线程安全性分析
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...
- Spring中获取request的几种方法,及其线程安全性分析(山东数漫江湖)
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性. 原创不易,如果觉得文章对你有帮助,欢迎点赞.评论.文章有疏漏之处,欢迎批评指正. 欢迎 ...
- Spring中如何获取request的方法汇总及其线程安全性分析
前言 本文将介绍在Spring MVC开发的web系统中,获取request对象的几种方法,并讨论其线程安全性.下面话不多说了,来一起看看详细的介绍吧. 概述 在使用Spring MVC开发Web系统 ...
- ConcurrentHashMap和 CopyOnWriteArrayList提供线程安全性和可伸缩性 以及 同步的集合类 Hashtable 和 Vector Collections.synchronizedMap 和 Collections.synchronizedList 区别缺点
ConcurrentHashMap和 CopyOnWriteArrayList提供线程安全性和可伸缩性 DougLea的 util.concurrent 包除了包含许多其他有用的并发构造块之外,还包含 ...
- 我是怎样测试Java类的线程安全性的
线程安全性是Java等语言/平台中类的一个重要标准,在Java中,我们经常在线程之间共享对象.由于缺乏线程安全性而导致的问题很难调试,因为它们是偶发的,而且几乎不可能有目的地重现.如何测试对象以确保它 ...
- 分享和探讨——如何测试Java类的线程安全性?
缺乏线程安全性导致的问题很难调试,因为它们是零星的,几乎不可能有意复制.你如何测试对象以确保它们是线程安全的? 我在最近的学习中和优锐课老师谈到了这个问题.现在,是时候以书面形式进行解释了.线程安全是 ...
- Vector线程安全,ArrayList非线程安全
http://baijiahao.baidu.com/s?id=1638844080997170869&wfr=spider&for=pc Vector线程安全,ArrayList非线 ...
随机推荐
- 【Unity3D技巧】一个简单的Unity-UI框架的实现
如何使用 请直接导入UnityUIFramework这个UnityPackage,然后进入名为Test的Scene即可开始体验各种特性,Enjoy!你可以通过访问我的Github进行查阅和下载. Vi ...
- Unity3D深入浅出 - 新版粒子系统 (Shuriken) - Tonge
Shuriken粒子系统是继Unity3.5版本之后推出的新版粒子系统,它采用了模块化管理,个性化的粒子模块配合粒子曲线编辑器使用户更容易创作出各种兵分复杂的粒子效果. 创建一个粒子系统的方式有两种: ...
- Git图文教程:从零到上传GitHub项目
一:安装Git 从Git官网下载.安装客户端 二:本地建立代码仓库 在开始菜单中找到 Git Bash 并打开 配置身份 git config --global user.name "pen ...
- 怎么让OCR文字识别软件转换别的语言文档
ABBYY PDF Transformer+让您可创建或转换希伯来语.意第绪语.日语.中文.泰语.韩语和阿拉伯语的文档.那么如何顺利使用这些复杂语言文字呢?小编教你两步骤轻松快速处理包含以下复杂语言文 ...
- 使用架构(XSD)验证XML文件
假使说XML是一个数据库,那么XSD就是这个数据库的结构.由此可见,XSD是如此重要,如果没有它,我们如何声明以及验证我们需要的XML数据文件的格式和合法性呢?那是不可能完成的任务,如果你将XML数据 ...
- awk笔记
http://www.cnblogs.com/zhuyp1015/archive/2012/07/14/2591842.html awk实例练习 http://www.cnblogs.com/repo ...
- cocos2d-x,求世界坐标
老版: http://user.qzone.qq.com/350479720/blog/1384483239 一,求node的世界坐标.因为node的contentSize为0,局部坐标原点与node ...
- CSS实例:鼠标滑过超级链接文字时改变背景颜色
先讲简单的: 通过CSS可以设置超链接在不同时刻的颜色: <style> a:link {color: #FF0000} /* 未访问的链接 */ a:visited {color: #0 ...
- 【转】windows7的桌面右键菜单的“新建”子菜单,在注册表哪个位置,如何在“新建"里面添加一个新项
点击桌面,就会弹出菜单,然后在“新建”中就又弹出可以新建的子菜单栏.office与txt 的新建都是在这里面的.我想做的事情是:在右键菜单的“新建” 中添加一个“TQ文本”的新建项,然后点击它之后,桌 ...
- 【Reporting Services 报表开发】— 交互式报表
我们知道,界面是人与系统间的对话方式,当使用者面对的是冷冰冰的界面,不但会造成使用者对于系统的热情减低,也会因为不便而产生诸多抱怨.尤其像报表时企业内几乎每日都会使用到的工具,因此,如何让使用者可以再 ...