首页 > 编程语言 >java-并发集合-并发hash表 ConcurrentHashMap 演示

java-并发集合-并发hash表 ConcurrentHashMap 演示

时间:2022-10-28 14:34:01浏览次数:60  
标签:ConcurrentHashMap java concurrent int util 并发 key new


java-并发集合-并发hash表 ConcurrentHashMap 演示

package me.grass.demo.concurrent;


import java.util.Date;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;


/**
* 并发集合 java.util.concurrent.ConcurrentHashMap 演示
* 并发 10 个生产者产生数据,并发 5 个消费消费数据;调整活动线程数将减小处理时间
* @author xxj
*/
public class ConcurrentHashMapDemo {
/**
* 生产者快,消费者慢
* @param args
* @throws InterruptedException
* @author xxj
*/
public static void main(String[] args) throws InterruptedException {
Date before=new Date();
int pTotalThread =100; //最大线程数(生产者)
int pActivities=10; //最大线程数(生产者)
int cTotalThread =50; //活动线程数(消费者)
int cActivities=5; //活动线程数(消费者)
_lCountDownLatch = new CountDownLatch(pTotalThread+cTotalThread);

initKeys(pTotalThread);
startProducer(pActivities,pTotalThread);
startConsumer(cActivities,cTotalThread);

_lCountDownLatch.await();//等待所有线程完成
Date after = new Date();


System.out.println("队列为空:"+_concurrentHashMap.isEmpty());
System.out.println("耗时:"+((after.getTime()-before.getTime())/1000));
System.out.println("同步队列:"+_lCountDownLatch.getCount());
}
private static java.util.concurrent.CountDownLatch _lCountDownLatch;
private static java.util.concurrent.ConcurrentHashMap<Integer,Integer> _concurrentHashMap =
new java.util.concurrent.ConcurrentHashMap<Integer,Integer>();
private static java.util.concurrent.ConcurrentLinkedQueue<Integer> _keys =
new ConcurrentLinkedQueue<Integer>();

private static void initKeys(int size){
for(int i=0;i<size;i++){
_keys.add(i);
}
}
private static void startProducer(int active,int totalThread) throws InterruptedException{
java.util.concurrent.ExecutorService pool = Executors.newFixedThreadPool(active);
int size =1024*1024*10;//产生 3 M 数据
Thread thread ;
for(int i=0;i<totalThread;i++){
thread = new Thread(new producer(i,size));
pool.execute(thread);
}
}
private static void startConsumer(int active,int totalThread) throws InterruptedException{
java.util.concurrent.ExecutorService pool = Executors.newFixedThreadPool(active);
Thread thread ;
//启动x个消费者
for(int i=0;i<totalThread;i++){
thread = new Thread(new consumer());
pool.execute(thread);
}
}
/**
* 生产者
* @author xxj
*
*/
private static class producer implements Runnable{
public producer(int key,int size){
_size=size;
_key=key;
}
int _key;
int _size;
public void run() {
ConcurrentHashMapDemo._concurrentHashMap.put(_key,_key);//生产
System.out.println("已创建:"+_key);
ConcurrentHashMapDemo._lCountDownLatch.countDown();//线程同步递减
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 消费者
* @author xxj
*
*/
private static class consumer implements Runnable{
public consumer(){
}
public void run() {
Integer key = 0;
Integer nInteger=null;
//循环消费,直到队列内容为空
while(!ConcurrentHashMapDemo._concurrentHashMap.isEmpty() &&
!ConcurrentHashMapDemo._keys.isEmpty()){
key = ConcurrentHashMapDemo._keys.poll();//获取 key
if(ConcurrentHashMapDemo._concurrentHashMap.containsKey(key)){
nInteger = ConcurrentHashMapDemo._concurrentHashMap.get(key);//消费
}
System.err.println("消费:"+nInteger);
try {
Thread.sleep(500);//每次消费等一会儿
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ConcurrentHashMapDemo._lCountDownLatch.countDown();//线程同步递减
}
}
}



标签:ConcurrentHashMap,java,concurrent,int,util,并发,key,new
From: https://blog.51cto.com/u_4518216/5804860

相关文章

  • java-并发集合-并发队列 ConcurrentLinkedQueue 演示
    java-并发集合-并发队列ConcurrentLinkedQueue演示目标:模拟5个线程同时并发读取“并发队列”,并使用CountDownLatch类协助计算消费耗时。pack......
  • 使用jvmti dll |so 加密java class jar包
    dll代码 //dllmain.cpp:定义DLL应用程序的入口点。#include"pch.h"#include<iostream>#include<jni_md.h>#include<jni.h>#include<jvmti.h>#include......
  • Java集合
    List和Set的区别:List:有序,按对象进入的顺序保存对象,可重复,允许多个Null元素对象,可以使用Iterator取出所有元素,再逐一遍历,还可以使用get(intindex)获取指定下标的元素......
  • Java程序员就业方向主要有哪几个?
    1、Android开发Android是全球最大的智能手机操作系统,根据StrategyAnalytics最新研究报告显示,全球智能手机出货量在2016年第三季度达到3.75亿台。Android操作系统获得了创......
  • 【JavaSE】Java常用类
    1.String的特性代表字符串,java中所有字符串字面值都作为此类的实现例实现。String是一个final类,不能被继承。String实现了Serialiable,表示字符串支持序列化,实现了Comarabl......
  • Java™ Management Extensions Technology Stack
    JavaPlatform,StandardEditionJavaManagementExtensionsGuideJava™ManagementExtensionsInstrumentationandAgentSpecification,v1.2Java™ManagementEx......
  • java commond
    #!/bin/bash#参数配置#jar包路径jarPath=/app/beifa/20221008#jar包名称jarName=htsc-svc-data-migration-10-08#log日志输出路径logPath=/app/betalpha/htsc/log......
  • java基础-->源码,反码,补码 和位运算
    原码、反码、补码原码:十进制数据的二进制表现形式,最左边的是符号位,0为正,1为负。反码:正数的反码是其本身,负数的反码是符号位保持不变,其余取反。补码:正数的补码是其本身,......
  • Kotlin Jetpack 实战|00. 写给 Java 开发者的 Kotlin 入坑指南
    简介本文主要讲解Kotlin​​基础语法​​。本文是​​《KotlinJetpack实战》​​的开篇。主要内容每个Java开发者都应该学Kotlin快速认识Kotlin基础语法扩展函数委......
  • java第二天 随机数
    Random的包先生成实例Randomrm=newRandom();newRandom().nextInt();--返回int范围内的一个随机整数newRandom().nextInt(n);--返回0~n之间的一个随机整......