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非线 ...
随机推荐
- RandomAccessFile
RandomAccessFile是用来访问那些保存数据记录的文件的,你就可以用seek( )方法来访问记录,并进行读写了.这些记录的大小不必相同:但是其大小和位置必须是可知的.但是该类仅限于操作文件
- 大白话系列之C#委托与事件讲解(三)
今天我接着上面的3篇文章来讲一下,为什么我们在日常的编程活动中遇到这么多sender,EventArgs e 参数:protected void Page_Load(object sender, Ev ...
- 旋转camera到特定对象
设定一个物体使得camera可以从现在为止自动飞到当前位置 1. 设定一个位置,可以在其前方放置一个显示其位置的cube.这里也可以写脚本设定位置. 2. 使用函数 移动函数 transform.po ...
- JSP 相关试题(二)
填空题 1.W3C是指___万维网联盟_______. 2.Internet采用的通信协议是___TCP/IP___. 3.当今比较流行的技术研发模式是__C/S__和__B/S__的体系结构来实现的 ...
- sqlserver模糊查询【转】
http://blog.csdn.net/liuxinxin1125/article/details/5444873 SELECT * FROM user WHERE name LIKE ';%三%' ...
- HDFS 文件读写过程
HDFS 文件读写过程 HDFS 文件读取剖析 客户端通过调用FileSystem对象的open()来读取希望打开的文件.对于HDFS来说,这个对象是分布式文件系统的一个实例. Distributed ...
- each和$(this)配合循环_siblings选取同级不同类型元素
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- jmeter使用IP欺骗压力测试
最近在使用jmeter进行压力测试时需要使用类似于loadrunner的IP欺骗功能,经问津度娘无果后决定再次耐心研究jmeter官方文 档,终于发现在jmeter2.5以上的版本有此功能的实现,由于 ...
- window.location.search
http://i.cnblogs.com/EditPosts.aspx?opt=1&opt2=x 就拿上面这个URL来说window.location.search的返回值为opt=1& ...
- mysql中int转varchar
这里要注意,cast(XX as varcahr(10))在mysql中不好使,要cast(XX as char(10))这样才好使