下面这样写法是Vector线程不安全的写法:

import java.util.Vector;

public class Test {
private static Vector<Integer> vector = new Vector<Integer>(); public static void main(String[] args) {
while (true) {
for (int i = 0; i < 10; i++) {
System.out.println("添加");
vector.add(i);
} Thread removeThread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < vector.size(); i++) {
System.out.println("removeThread删除");
vector.remove(i);
}
}
}); Thread printThread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < vector.size(); i++) {
System.out.println("printThread获取"); System.out.println((vector.get(i)));
}
}
}); removeThread.start();
printThread.start(); //不要同时产生过多的线程,否则会导致操作系统假死
while (Thread.activeCount() > 20);
}
}
  }
}

  尽管Vector get()、remove()、get() 方法是I同步的 但运行上面程序会出现以下错误:

java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
at java.util.Vector.get(Vector.java:744)
at Test$2.run(Test.java:29)
at java.lang.Thread.run(Thread.java:722)
Exception in thread "Thread-14857" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
at java.util.Vector.get(Vector.java:744)
at Test$2.run(Test.java:29)

HashTable线程不安全写法:

import java.util.Hashtable;
import java.util.Map; public class HashmapTest { private static Map<Integer,Integer> hashtable= new Hashtable<Integer,Integer>();
public static void main(String[] args) {
while(true){
for (int i = 0; i < 10; i++) {
System.out.println("添加");
hashtable.put(i, i);
}
Thread removeThread = new Thread(new Runnable() {
@Override
public void run() {
Iterator it = hashtable.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<integer integer=""> entry=(Entry<integer integer="">) it.next();
System.out.println("delete this: "+entry.getKey()+"==="+entry.getValue());
it.remove();
}
} Thread getThread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < hashtable.size(); i++) {
System.out.println("getThread获取");
System.out.println((hashtable.get(i)));
}
}
});
removeThread.start();
getThread.start();
while (Thread.activeCount() > 20);
}
}
}

  会出现很多null值,但不错,因为没有那个key ,但不会报错

getThread获取
null
getThread获取
null

  在多线程环境中,如果不在方法调用端做额外的同步措施,使用这段仍是线程不安全的,因为如果一个线程恰好再错误的时间删除了一个元素, 导致i不在可用的话,get方法会抛出一个ArrayIndexOutOfBoundsException

import java.util.Vector;

public class Test {
private static Vector<Integer> vector = new Vector<Integer>(); public static void main(String[] args) {
while (true) {
for (int i = 0; i < 10; i++) {
System.out.println("添加");
vector.add(i);
} Thread removeThread = new Thread(new Runnable() {
@Override
public void run() {
synchronized (vector) {
for (int i = 0; i < vector.size(); i++) {
System.out.println("removeThread删除");
vector.remove(i);
}
}
}
}); Thread printThread = new Thread(new Runnable() {
@Override
public void run() {
synchronized (vector) {
for (int i = 0; i < vector.size(); i++) {
System.out.println("printThread获取"); System.out.println((vector.get(i)));
}
}
}
}); removeThread.start();
printThread.start(); //不要同时产生过多的线程,否则会导致操作系统假死
while (Thread.activeCount() > 20);
}
}
}

Vector、HashTable线程不安全示例的更多相关文章

  1. C++并发编程 条件变量 condition_variable,线程安全队列示例

    1. 背景 c++11中提供了对线程与条件变量的更好支持,对于写多线程程序方便了很多. 再看c++并发编程,记一下学习笔记. 2. c++11 提供的相关api 3.1 wait wait用于无条件等 ...

  2. Python自定义线程类简单示例

    Python自定义线程类简单示例 这篇文章主要介绍了Python自定义线程类,结合简单实例形式分析Python线程的定义与调用相关操作技巧,需要的朋友可以参考下.具体如下: 一. 代码     # - ...

  3. HashMap和Hashtable 线程安全性

    HashMap和Hashtable的比较是Java面试中的常见问题,用来考验程序员是否能够正确使用集合类以及是否可以随机应变使用多种思路解决问题.HashMap的工作原理.ArrayList与Vect ...

  4. C# 集合 — Hashtable 线程安全

    基础知识重要吗?真的很重要. 就在笔者与同事聊天中突然同事提出一个问题,让笔都有点乱了手脚(有点夸张),题目是这样的: 问:Hashtable 是线程安全的吗? 答:…… (沉默中,Yes Or No ...

  5. vector(相对线程安全) arryList(线程不安全)

    1.什么是线程安全? 如果说某个集合是线程安全的,那么我们就不用考虑并发访问这个集合?(需要定义自己百度,但是很难懂) 2.深入jvm中的线程安全的级别. a不变模式(String等基本类型) b.绝 ...

  6. iOS 多线程之线程锁Swift-Demo示例总结

    线程锁是什么 在前面的文章中总结过多线程,总结了多线程之后,线程锁也是必须要好好总结的东西,这篇文章构思的时候可能写的东西得许多,只能挤时间一点点的慢慢的总结了,知道了线程之后要了解线程锁就得先了解一 ...

  7. Java中java.util.concurrent包下的4中线程池代码示例

    先来看下ThreadPool的类结构 其中红色框住的是常用的接口和类(图片来自:https://blog.csdn.net/panweiwei1994/article/details/78617117 ...

  8. sonar——Synchronized classes Vector, Hashtable, Stack and StringBuffer should not be used

    It is better to use their new unsynchronized replacements: ArrayList or LinkedList instead of Vector ...

  9. Vector 是线程安全的,是不是在多线程下操作Vector就可以不用加Synchronized

    如标题一样,如果之前让我回答,我会说,是的,在多线程的环境下操作Vector,不需要加Synchronized. 但是我今天无意间看到一篇文章,我才发现我之前的想法是错误的,这篇文章的地址: http ...

随机推荐

  1. Qt编写自定义控件24-图片轮播控件

    一.前言 上一篇文章写的广告轮播控件,采用的传统widget堆积设置样式表做的,这次必须要用到更高级的QPainter来绘制了,这个才是最高效的办法,本控件参考雨田哥的轮播控件,经过大规模的改造而成, ...

  2. EasyUI之toolTip

    <a class="easyui-tooltip" title="提示框" href="http://www.baidu.com"&g ...

  3. 推荐一个加载动图的网站loading.io

    推荐一个非常好玩的loading gif的资源网站:https://loading.io/ 里面有各种loading的动图.

  4. 利用工具破解HTTP身份验证的多种方法

    https://www.hackingarticles.in/multiple-ways-to-exploiting-http-authentication/ 1)场景 利用Apache配置HTTP验 ...

  5. ubuntu 16 搭建只能上传不可下载删除ftp服务

    安装 VSFTPD,(建议使用FileZill测试,报错能看到原因) 如果使用window文件管理连接,要注意下图的设置 使用 apt-get 安装 vsftpd sudo apt-get insta ...

  6. 【VS开发】VC下加载JPG/GIF/PNG图片的两种方法

    1.用API OleLoadPicture来加载JPG.GIF格式的图片(注:不支持PNG格式,另外GIF只能加载第一帧,且不支持透明) OleLoadPicture 函数实际上创建了一个IPictu ...

  7. JavaScript 检测值

    了解常见的真值和假值,可以增强判断能力.在使用if判断时,提升编码速度. 了解常见的检测和存在,一样可以增强判断能力,而且是必须掌握的. 数组和对象被视为真值 1 2 3 4 5 6 7 8 9 10 ...

  8. Reactor系列(二)Flux Mono创建

    Flux Mono创建 视频讲解:https://www.bilibili.com/video/av78844777/ FluxMonoTestCase.java package com.exampl ...

  9. find the mincost route【无向图最小环】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1599 Problem Description 杭州有N个景区,景区之间有一些双向的路来连接,现在860 ...

  10. POJ3311 Hie with the Pie 【状压dp/TSP问题】

    题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total ...