簡體   English   中英

Java Hibernate-使多個線程同時運行

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM