我有一个由多个线程调用的方法。
class Supplier {
String name
....
static hasMany = [supplierDetailInfos : SupplierDetailInfo]
static searchable = {
only = ['name', 'supplierDetailInfos']
supplierDetailInfos component: true
}
}
class SupplierDetailInfo {
SupplierDetail supplierDetail
Supplier supplier
String value
static searchable = {
only = ['id', 'supplierDetail', 'value']
supplierDetail component: true
}
}
class SupplierDetail {
String name
....
static searchable = {
only = ['id']
}
}
我有另一种方法可供调度程序访问,并且每月只运行一次,即它会重置玩家的每月排名数据。
public void notifyCompleted(PlayerDTO winner, PlayerDTO loser) {
PlayerRankedData winnerData = rankedDataMap.get(winner.getId());
PlayerRankedData loserData = rankedDataMap.get(loser.getId());
int pointsToAdd = getPointsToAdd(winnerData, loserData);
int pointsToDeduct = getPointsToDeduct(loserData, winnerData);
winnerData.addMatchPoints(pointsToAdd);
loserData.deductMatchPointsUptoMinimum(pointsToDeduct, minimumMatchPoints);
winnerData.addRewardTokens(tokenPerVictory);
}
我想要的是,当private void resetLadder() {
for (PlayerRankedData playerRankedData : rankedDataMap.values()) {
int monthlyTokens = playerRankedData.getRewardTokens() * calculateTierMultiplier(playerRankedData);
playerRankedData.setUnlockedTokens(monthlyTokens);
playerRankedData.resetMonthlyDataData(); //This updates reward tokens, matchPoints and other variables which are modified inside notifyCompleted.
updateOnDatabase(playerRankedData);
}
}
正在运行时,resetLadder
会等到notifyCompleted
完成执行。
我可以通过使用锁定对象并锁定resetLadder
和notifyCompleted
的主体来使其工作,但我不想这样做,因为那时只有1个线程可以访问{{ 1}}在我不想要的时间。它完全适合多线程访问notifyCompleted。
我认为忙于等待AtomicBoolean可以使其工作,但问题是如果resetLadder
正在运行且notifyCompleted
开始运行,那么它将导致状态不一致。
所以我的问题是,有没有办法让notifyCompleted
运行,但只有在resetLadder运行时才会被阻止,而当resetLadder
未运行时,resetLadder将无法启动。