Concurrent Processing - Record your own process

With the increase of users, frequency of concurrent phenomenon more and more, this time if not done concurrent processing, will result in inconsistent data, then you need to increase it locks the needs of different users to only one object at the same time operation, in order to better scalability, himself wrote a class for managing concurrent requests, specific Psion Teklogix is that every request over the object to obtain a unique key to ensure that the request to obtain the same treatment is key after the same object, the same object and in its related operations, a process only one, with the synchronized keyword can be limited, probably a little schematic drawing, each line corresponding to a request to run release
Here Insert Picture Description
Here is the code that implements the management class:

package com.mx.util;

import java.util.HashMap;

public class MxObjectLockUtil {
    private   HashMap<String,PlanLock> planLockHashMap = new HashMap<>(40);
    private static volatile MxObjectLockUtil instance;

    private MxObjectLockUtil(){

    }

    public HashMap<String, PlanLock> getPlanLockHashMap() {
        return planLockHashMap;
    }

    public synchronized PlanLock getObjectLock(String key){
        if (planLockHashMap.get(key) == null){
            PlanLock planLock = new PlanLock();
            planLockHashMap.put(key,planLock);
            System.err.println("生成:" + key);
            return planLock;
        }
        return planLockHashMap.get(key);
    }

    public  void removeLock(String key){
        if (planLockHashMap.get(key) != null){
            PlanLock planLock =  planLockHashMap.remove(key);
            System.err.println("移除:" + key + ";对象:" + planLock);
        }
    }

    public static MxObjectLockUtil getInstance(){
        if (instance == null){
            synchronized (MxObjectLockUtil.class){
                if (instance == null){
                    instance = new MxObjectLockUtil();
                }
            }
        }
        return instance;
    }

    class PlanLock{
        public synchronized void run(Runnable runnable){
            runnable.run();
        }
    }

}


Then write a test class, to see results

package com.mx.util;

import java.util.Random;

public class Test {
	public static MxObjectLockUtil mxObjectLockUtil;

	public static void main(String[] args) {
		mxObjectLockUtil = MxObjectLockUtil.getInstance();
		Random random = new Random();
		for (int i = 0; i < 10; i++) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					String key = random.nextInt(1) + "";
					mxObjectLockUtil.getObjectLock(key).execute(new sleepRunabble(key, 400));
					mxObjectLockUtil.removeLock(key);
				}
			}).start();
		}
	}

	static class sleepRunabble implements Runnable {
		private String id;
		private long times;

		sleepRunabble(String id, long times) {
			this.id = id;
			this.times = times;
		}

		@Override
		public void run() {
			System.out.println("我是" + id);
			try {
				Thread.sleep(times);
				System.out.println(id + "延迟" + times + "ms");
				System.err.println(mxObjectLockUtil.getPlanLockHashMap().size());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

This is a test 10 threads operating a target when
Here Insert Picture Description
normal, and did not appear at the same time I was number 0 or the same number in the delay,
the following test while 1000 threads, the situation of 200 objects operation, test code is as follows:

package com.mx.util;

import java.util.Random;

public class Test {
	public static MxObjectLockUtil mxObjectLockUtil;

	public static void main(String[] args) {
		mxObjectLockUtil = MxObjectLockUtil.getInstance();
		Random random = new Random();
		for (int i = 0; i < 10000; i++) {
			new Thread(new Runnable() {
				@Override
				public void run() {
					String key = random.nextInt(200) + "";
					mxObjectLockUtil.getObjectLock(key).execute(new sleepRunabble(key, 50));
					mxObjectLockUtil.removeLock(key);
				}
			}).start();
		}
	}

	static class sleepRunabble implements Runnable {
		private String id;
		private long times;

		sleepRunabble(String id, long times) {
			this.id = id;
			this.times = times;
		}

		@Override
		public void run() {
			System.out.println("我是" + id);
			try {
				Thread.sleep(times);
				System.out.println(id + "延迟" + times + "ms");
				System.err.println(mxObjectLockUtil.getPlanLockHashMap().size());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

Here Insert Picture Description
Still do not simultaneously the same number in the delay or generated, fully describe this method is feasible, efficiency also can, when 10,000 pairs 200 objects operation request sent, almost average 50 threads is the same object, these 50 objects in order to get the same object, and then followed by the implementation, the main consuming, just, just beginning to obtain assignment, follow the basic objects is 200 not operate simultaneously

If better suggestions or wrong place, please comment out, I also continue to learn and improve, only to a higher technology, better tomorrow, O (∩_∩) O haha ​​~

Published 36 original articles · won praise 13 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_41392105/article/details/102915800