[英]Java Hibernate - Making multiple threads run simultaneously
我正在尝试通过从多个线程中调用以下方法来测试SPRING MVC服务的性能。 该方法使用Hibernate来获取一条记录,然后再次对其进行更新。 但是似乎所有线程都按顺序执行,而不是并行执行。
我的服务方式
@Transactional
public String performOperation() {
USER user = dao.findUsr("name");
user.setMarks(50);
}
我的测试应用
*Round 1*
thread1.start() : For Only T1, It takes time to execute : 5 Sec
*Round 2*
thread1.start()
thread2.start() : With T1 and T2: 10 Sec
*Round 3*
thread1.start()
thread2.start()
thread3.start() : With T1, T2, T3: 15 sec
*Round 4*
thread1.start()
thread2.start()
thread3.start()
thread4.start() : With T1, T2, T3, T4: 20 sec
我的配置
jdbc.initial.pool.size=10
jdbc.min.pool.size=10
jdbc.max.pool.size=120
没有为以下设置设置任何内容 :因此采用默认值
- current_session_context_class
- cache
观察:即使对于每个线程5000个循环,使用的最大数据库池大小也是25。在MySQL仪表板中观察到
问题如果您看到它没有并行执行。 我猜,Hibernate正在锁定该行。 您能否提供任何指针以同时运行它。
要在完全相同的时间启动线程,我建议尝试CyclicBarrier 。
CyclicBarrier是“一种同步辅助工具,它允许一组线程互相等待以到达一个公共的障碍点”。
该文档中有一个示例,说明了它如何工作和运行。 在您的代码中进行引用。 例如:
final CyclicBarrier gate = new CyclicBarrier(5);
Thread thread1 = new Thread(){
public void run(){
gate.await(); // Waits until all parties have invoked await on this barrier.
//Your Code
}};
Thread thread2 = new Thread(){
public void run(){
gate.await();
//Your Code
}};
Thread thread3 = new Thread(){
public void run(){
gate.await();
//Your Code
}};
Thread thread4 = new Thread(){
public void run(){
gate.await();
//Your Code
}};
thread1.start();
thread2.start();
thread3.start();
thread4.start();
gate.await();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.