Technology sharing: Java multi-threaded learning to deal with high concurrency problems

In the application of the program, the number of users or requests reaches a certain number, and concurrent requests cannot be avoided. Since each call to the interface must be terminated upon return, if the business of the interface is relatively complex, there may be more Users. When calling the interface, the user will be frozen.

1

The following content will introduce a clever and very simple way to solve this problem.

Request to write to memory

2

We can encapsulate each request as an object and then write it to memory.

@Getter
@Setter
Private static requested object class CallVo {//
private Long param1; // parameters. 1
Private param2 Integer; // Parameter 2
}
Private List <CallVo> = new new callVoList the ArrayList <> (); // request a list of objects
/ **
* Interface call
* @param callVo
* @return
*/
@Transactional
@RequestMapping(value = "/call",method = {RequestMethod.POST})
public void call(@RequestBody CallVo callVo){
synchronized (syncObject) {
callVoList .add(callVo);//Write the requested object into memory
}
}
Multi-threaded processing

创建一个线程以继续读取内存中的数据. 如果被请求对象的收集长度为0多线程技术问题解决,则表明没有请求. 如果集合中有数据,请从集合中删除请求的对象并获取时间. 根据相应业务处理请求的参数.

这达到了将同步转变为异步的目的,并简单地解决了高并发性的问题.

private Thread thread;
private final Object syncObject = new Object(); // 同步对象
private volatile boolean runnable;
@Override
public void run() {
while (runnable){
try {
if (callVoList.size() == 0) {//集合长度为0,证明没有请求
Thread.sleep(1000);
continue;
}
CallVo callVo;
synchronized (syncObject) {
callVo = callVoList.remove(0);
}
Long param1 = callVo.getStationId();
Integer param2 = callVo.getCallType();

//业务处理
System.out.println(param1+"---"+param2);

}catch (Exception e){
logger.error("接口调用异常:", e);
}
}
}

完整代码:

@RestController
@Slf4j
@RequestMapping("/erpRemote")
public class ErpRemoteController extends BaseController implements DisposableBean, Runnable{

ErpRemoteController(){
callVoList = new ArrayList<>();
runnable = true;
thread = new Thread(this);
thread.start();
}
/**
* 接口调用
* @param callVo
* @return
*/
@Transactional
@RequestMapping(value = "/call",method = {RequestMethod.POST})
public GeneralResponse call(@RequestBody CallVo callVo){

synchronized (syncObject) {
callVoList.add(callVo);//将请求对象写入内存
}
return GeneralResponse.success(null);
}
@Getter
@Setter
private static class CallVo{//请求对象
private Long param1;//参数1
private Integer param2;//参数2
}
private List<CallVo> callVoList;
private Thread thread;
private final Object syncObject = new Object(); // 同步对象
private volatile boolean runnable;
@Override
public void run() {
while (runnable){
try {
if (callVoList.size() == 0) {//集合长度为0,证明没有请求
Thread.sleep(1000);
continue;
}
CallVo callVo;
synchronized (syncObject) {
callVo = callVoList.remove(0);
}
Long param1 = callVo.getStationId();
Integer param2 = callVo.getCallType();

//业务处理
System.out.println(param1+"---"+param2);

}catch (Exception e){
logger.error("Interface call exception:", e);
}
}
}
@Override
public void destroy() {
runnable = false;
}
}
Restrictions

Because synchronization is asynchronous, the return value of each request interface is the only solution to the multi-threaded technical problem, so this method is only suitable for the case where the requester does not need to return.

The above is the content of Java multithreading learning to deal with high concurrency problems introduced by Xiaoqian, I hope it will be helpful to everyone.

This article is from Qianfeng Education , please indicate the source for reprinting.


Guess you like

Origin blog.51cto.com/15128702/2676976