Java synchronizing based on a parameter (named mutex/lock)

后端 未结 8 1797
逝去的感伤
逝去的感伤 2020-12-05 02:50

I\'m looking for a way to synchronize a method based on the parameter it receives, something like this:

public synchronized void doSomething(name){
//some co         


        
8条回答
  •  情书的邮戳
    2020-12-05 03:00

    Check out this framework. Seems you're looking for something like this.

    public class WeatherServiceProxy {
    ...
    private final KeyLockManager lockManager = KeyLockManagers.newManager();
    
    public void updateWeatherData(String cityName, Date samplingTime, float temperature) {
            lockManager.executeLocked(cityName, new LockCallback() {
                    public void doInLock() {
                            delegate.updateWeatherData(cityName, samplingTime, temperature);
                    }
            });
    }
    

    https://code.google.com/p/jkeylockmanager/

提交回复
热议问题