java - 服务和控制器之间打开多线程有什么区别?
问题描述
服务和控制器之间打开多线程有什么区别?当我在控制器中打开多线程时,我不需要打开休眠会话,我可以得到currentSession
. 但是,当我在服务中打开一个线程时,我必须先获取hibernate sessionFactory,然后再打开一个新会话。最后,我必须关闭会话。请帮助或尝试提供一些想法如何实现这一目标。
@Controller
public class TestController extends BaseController {
@Autowired
private TestService testService;
@Autowired
private ThreadPoolTaskExecutor taskExecutor;
@Autowired
private TestDao testDao;
@ResponseBody
@RequestMapping("/testMultiInService")
public Object test() {
GenericListResponse response = testService.testMulti();
return response;
}
}
@Service
public class TestServiceImpl extends TestService {
@Autowired
private ThreadPoolTaskExecutor taskExecutor;
@Autowired
private TestDao testDao;
private static Logger logger = Logger.getLogger(TestServiceImpl.class);
@Override
@Transactional
public GenericListResponse testMulti() {
CountDownLatch latch = new CountDownLatch(2);
TestCallable1 callable1 = new TestCallable1(this, latch);
Future<List> future1 = taskExecutor.submit(callable1);
List list = new ArrayList();
try {
latch.await();
if (null != future1)
list.addAll(future1.get());
if (null != future2)
list.addAll(future2.get());
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class TestCallable1 implements Callable<List> {
private TestService testService;
private CountDownLatch latch;
public TestCallable1(TestService testService, CountDownLatch latch) {
this.testService = testService;
this.latch = latch;
}
@Override
public List call() throws Exception {
List list = null;
try {
list = testService.getDimArea();
} catch (Exception e) {
e.printStackTrace();
} finally {
this.latch.countDown();
return list;
}
}
}
解决方案
推荐阅读
- python - bash - 将变量传递给 shell 文件中的字符串
- java - 如何在 for 循环中使用执行程序服务调用 CompletableFuture supplyAsync(()
- python - 在 pandas 上处理多个 csv 文件
- java - 在实例之间共享内存中的 java 对象
- html - 为什么我的下拉输入箭头不显示?
- dns - 在一个地区注册 IPV4 公网地址,在另一个地区使用
- excel - 根据单元格的背景颜色亮度自动设置字体颜色
- r - 计算R中多个组的平均值
- android - 尝试在 android (Nativescript-vue) 上使用 RadListView 时出现 Webpack 错误
- python - 根据文件名将文件从一个文件夹分隔到另一个文件夹