java - api客户端调用的线程安全
问题描述
让我们考虑以下简化代码:
@Service
public class Runner {
private Service service;
public void process(){
Thread threadA = new Thread("New Thread") {
public void run(){
service.process();
}};
Thread threadB = new Thread("New Thread") {
public void run(){
service.process();
}};
threadA.start();
threadB.start();
}
}
@Service
public class Service {
private ApiClient apiClient;
public void process(){
..do something..
ResponseObject response = apiClient.callSomething();
..do something..
}
}
ApiClient、Serivce 和 Runner 是 Spring 创建的单例 bean,通过构造函数注入。现在让我们说,当 threadA 执行它的工作时, callSomething() 方法得到一点超时,同时 threadB 开始执行它的工作。threadA 和 threadB 都在等待 callSomething() 的返回,然后来自 threadA 执行的响应返回。是否有可能由于此超时,threadA 和 threadB 都将继续响应 threadA 请求?
解决方案
推荐阅读
- sqlite - 我可以从android应用程序中提取数据库吗?
- machine-learning - SVM 的数据应该被缩放、标准化还是两者兼而有之
- vue.js - Vue CLI CSS 预处理器选项:dart-sass VS node-sass?
- python - 当参考仅显示“javascript:return true;”时,如何使用 BeautifulSoup 下载 ZIP 文件?
- r - 是否可以在 R 个人库中为一个特定版本的 R 安装不同的包版本
- vbscript - 如何将所有子文件夹移动到另一个文件夹保留树?
- php - “类不存在。反射失败。” 延迟加载和对象存储
- r - R:grep 意外输出多个字符串匹配
- oracle - 在plsql中参数为空时使用
- c - 在 arm-linux 中使用 ptrace 获取寄存器