Java 高并发下的实践

Posted 茅坤宝骏氹

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 高并发下的实践相关的知识,希望对你有一定的参考价值。

一、使用的技术

HashMap

ConcurrentHashMap

Lock

ReadWriteLock

synchronized

 

二、一百万并发下的组合

ConcurrentLockMap

技术分享
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.scau.mk.map;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *
 * @author MK
 */
public class ConcurrentLockMap implements In {

    ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
    Lock lock = new ReentrantLock();

    @Override
    public void add(int a) {
        lock.lock();
        try {
            if (map.containsKey(a)) {
                map.put(a, 1 + map.get(a));
            } else {
                map.put(a, 1);
            }
        } finally {
            lock.unlock();
        }
    }

    @Override
    public int get(int a) {
        int as = map.get(a);

        return as;
    }

}
View Code

ConcurrentSynchronizedMap

技术分享
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.scau.mk.map;

import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * @author MK
 */
public class ConcurrentSynchronizedMap implements In  {

    ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();

    public void add(int a) {

        if (map.containsKey(a)) {
            synchronized((Integer)a){
              map.put(a, 1 + map.get(a));
            }
        } else {
            map.put(a, 1);
        }

    }

    public int get(int a) {
        int as = map.get(a);
        return as;
    }

}
View Code

LockHashMap

技术分享
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.scau.mk.map;

import java.util.HashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *
 * @author MK
 */
public class LockHashMap implements In {

    HashMap<Integer, Integer> map = new HashMap<>();
    Lock lock = new ReentrantLock();

    @Override
    public void add(int a) {
        lock.lock();
        try {
            if (map.containsKey(a)) {
                map.put(a, 1 + map.get(a));
            } else {
                map.put(a, 1);
            }
        } finally {
            lock.unlock();
        }
    }

    @Override
    public int get(int a) {
        int as = map.get(a);

        return as;
    }

}
View Code

ReadWriteHashMap

技术分享
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.scau.mk.map;

import java.util.HashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 *
 * @author MK
 */
public class ReadWriteHashMap implements In {

    HashMap<Integer, Integer> map = new HashMap<>();
    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    //390

    @Override
    public void add(int a) {
        lock.writeLock().lock();
        try {
            if (map.containsKey(a)) {
                map.put(a, 1 + map.get(a));
            } else {
                map.put(a, 1);
            }
        } finally {
            lock.writeLock().unlock();
        }
    }

    @Override
    public int get(int a) {
        int as = 0;
        lock.readLock().lock();
        try {
            as = map.get(a);
        } finally {
            lock.readLock().unlock();
        }
        return as;
    }

}
View Code

SynchronizedHashMap

技术分享
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.scau.mk.map;

import java.util.HashMap;

/**
 *
 * @author MK
 */
public class SynchronizedHashMap implements In {

    HashMap<Integer, Integer> map = new HashMap<>();
  

    @Override
    public synchronized void add(int a) {
       
            if (map.containsKey(a)) {
                map.put(a, 1 + map.get(a));
            } else {
                map.put(a, 1);
            }
        
    }

    @Override
    public synchronized int get(int a) {
        int as = map.get(a);
       
        return as;
    }

}
View Code

Adder

技术分享
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.scau.mk.map;

import java.util.Random;

/**
 *
 * @author MK
 */
public class Adder implements Runnable {

    In in;
    Random random = new Random();

    public Adder(In in) {
        this.in=in;
    }

    @Override
    public void run() {

        for (int i = 0; i < 1000; i++) {
            //in.add(random.nextInt());
            in.add(i);
            
        }

    }
}
View Code

Getter

技术分享
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.scau.mk.map;

import java.util.Random;

/**
 *
 * @author MK
 */
public class Getter implements Runnable {

    In in;
    Random random = new Random();

    public Getter(In in) {
        this.in=in;
    }

    @Override
    public void run() {

        for (int i = 0; i < 1000; i++) {
            //in.add(random.nextInt());
            in.get(i);
            
        }

    }
}
View Code

TestDemo

技术分享
package cn.edu.scau.mk.map;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author MK
 */
public class TestDemo {

    /*
        add:66945 get:68174
        add:26896 get:27872
        add:128779 get:127131
        add:99832 get:102323
        add:70762 get:70493
     */
    public static void main(String[] args) {
        In in = null;

        in = new ReadWriteHashMap();
        operate(in);
        in = new ConcurrentSynchronizedMap();
        operate(in);
        in = new ConcurrentLockMap();
        operate(in);
        in = new SynchronizedHashMap();
        operate(in);
        in = new LockHashMap();
        operate(in);
    }

    public static void operate(In in) {
        int poolSize = 1000000;
        Adder add = new Adder(in);
        ExecutorService threadPool = Executors.newFixedThreadPool(8);
        long start = System.currentTimeMillis();
        for (int i = 0; i < poolSize; i++) {
            threadPool.execute(add);
        }
        threadPool.shutdown();
        try {//等待直到所有任务完成
            threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES);
            long end = System.currentTimeMillis() - start;
            System.out.print("add:" + end);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Getter get = new Getter(in);
        threadPool = Executors.newFixedThreadPool(8);
        start = System.currentTimeMillis();
        for (int i = 0; i < poolSize; i++) {
            threadPool.execute(add);
        }
        threadPool.shutdown();
        try {//等待直到所有任务完成
            threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES);
            long end = System.currentTimeMillis() - start;
            System.out.println(" get:" + end);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
View Code

 

三、输出结果

ReadWriteHashMap                                   add:66945 get:68174
ConcurrentSynchronizedMap                    add:26896 get:27872
ConcurrentLockMap                                  add:128779 get:127131
SynchronizedHashMap                             add:99832 get:102323
LockHashMap                                           add:70762 get:70493






 












以上是关于Java 高并发下的实践的主要内容,如果未能解决你的问题,请参考以下文章

大型网站高并发下前后端常用解决方案总结java

Java自学!springboot~高并发下耗时操作的实现

java里,如何保证高并发下的数据安全

redis实现高并发下的抢购/秒杀功能

Java--HashMap 在高并发下引起的死循环

高并发下的Java数据结构(List,Set,Map,Queue)