thread37
来源:互联网 发布:安卓编程用什么软件 编辑:程序博客网 时间:2024/05/18 06:26
package com.neutron.t22;import java.util.*;import java.util.concurrent.*;/** * 写时复制容器 copy on write * 多线程环境下, 写时效率低,读时效率高 * 适合写少读多的情况 */public class T222CopyOnWrite { /** * 测试什么内容? * 创建1个容器,开启10个线程,每个线程向容器内加入10000个数据,来测试不同容器的执行时间 */ public static void main(String[] args) { List<String> lists = // new ArrayList<>(); // 并发会存在问题 // new Vector<>(); // 无并发问题,但是很慢 new CopyOnWriteArrayList<>(); // 没有并发问题,但是速度实在太慢 Random r = new Random(); Thread[] ths = new Thread[100]; for (int i = 0; i < ths.length; i++) { ths[i] = new Thread(() -> { for (int j = 0; j < 1000; j++) { lists.add("l" + r.nextInt(1000)); } }); } runAndComputeTime(ths); /* 使用ArrayList的执行结果,这是个错误的执行结果,目标应该是10w 执行:161 list size:98758 ------------------------------------- Vector和CopyOnWriteArrayList都没有问题 */ System.out.println("list size:" + lists.size()); } static void runAndComputeTime(Thread[] ths){ long start = System.currentTimeMillis(); Arrays.asList(ths).forEach(Thread::start); Arrays.asList(ths).forEach((thread) -> { try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } }); long end = System.currentTimeMillis(); System.out.println(end - start); }}
阅读全文
0 0
- thread37
- thread37
- js replace
- js获取当前日期时间“yyyy-MM-dd HH:MM:SS”
- Unity打包iOSFailed to resolve base type System.ServiceModel.Configuration.BehaviorExtensionElement for
- N32926移植电容触摸屏GT911驱动到内核
- Camera模块解析之驱动篇
- thread37
- git hook远程仓库实现网站的自动部署
- centos6防火墙相关命令
- c++远征之多态篇——虚函数及其实现原理
- jQuery点击li标签改变颜色,其他li标签不变
- 文章标题
- Android adb
- 导入导出excel
- spring组件扫描<context:component-scan/>使用详解