整数值同步

huangapple go评论67阅读模式
英文:

Integer value synchronization

问题

以下是您要翻译的内容:

我对多线程还不太了解,在处理相同类型的代码时遇到了一些问题。

我有以下代码来基于整数值进行锁定。

Integer key = mmybatisMapper.getkeyByUserName(userName);  // 使用MyBatis进行数据库调用以获取密钥
PostProcessingTask postProcessingTask = new PostProcessingTask(key, userName);
Thread thread = new Thread(postProcessingTask);
thread.start();

// 后处理代码

public class PostProcessingTask implements Runnable {

    private final Integer key;
    private final Integer name;

    private ConcurrentMap<Integer, Integer> locks = new ConcurrentHashMap<Integer, Integer>();

    public PostProcessingTask(final Integer key, final Integer name) {
        this.key = key;
        this.name = name;
        locks.put(key, key);
    }

    @Override
    public void run() {
        try {
            synchronized (locks.get(this.key)) {
                // 业务逻辑
            }

        }
    }
}

上述代码可以很好地实现基于整数值的同步。

然而,现在我又在尝试在另一个项目中基于整数值实现同步,但却不起作用。
上述代码和新项目代码之间唯一的区别是,上述代码从数据库中使用MyBatis获取整数值,而我的新代码将使用来自REST端点请求的整数值。

您能否帮助我解决根据REST端点请求中的值实现同步的问题?

英文:

I am new to multi-threading and getting some issues with the same kind of code.

I have the following code to lock based on an integer value.

Integer key = mmybatisMapper.getkeyByUserName(userName);  /// database call using mybatis to get key    
PostProcessingTask postProcessingTask = new PostProcessingTask(key,
					userName);
Thread thread = new Thread(postProcessingTask);
thread.start();

//Post processing code

public class PostProcessingTask implements Runnable {

	private final Integer key;
	private final Integer name;

	private ConcurrentMap&lt;Integer, Integer&gt; locks = new ConcurrentHashMap&lt;Integer, Integer&gt;();

	public PostProcessingTask(final Integer key, final Integer name) {
		this.key = key;
		this.name = name;
		locks.put(key, key);
	}

	@Override
	public void run() {
		try {
			synchronized (locks.get(this.key)) {	
					///business logic
			}			
				
		}
	}	
}

The above code is working fine to achieve synchronization based on an Integer value.

However, now I'm again trying to achieve the synchronization in another project based on an Integer value and it's not working.
The only difference between the above code and the new project code is that the above code is getting Integer value from DB using MyBatis and my new code will be using Integer value from the REST endpoint request.

Can you please help me to solve the issue to do synchronization based on the value from REST endpoint request?

答案1

得分: 2

使用synchronized关键字来同步一个Integer值是一个非常糟糕的想法,因为对于不在[-128, 127]范围内的整数,Integer.valueOf的行为是不同的,而且完全不相关的代码可能会在其上进行同步。在Integer值上进行同步可能会产生不可预测的怪异行为。

然而,有一种简单的方法可以修改你的代码以实现你想要做的事情,那就是仅使用一个毫无意义的Object来进行同步。可以写成locks.put(key, new Object()),然后保持代码的其余部分不变。

英文:

Doing synchronized on an Integer value is an extraordinarily bad idea, as Integer.valueOf has different behavior for integers outside [-128, 127], and completely unrelated bits of code might synchronize on it. Synchronizing on an Integer value is likely to do unpredictably weird things.

There is a simple way of modifying your code to do what you're trying to do, though, and that is to use just a completely meaningless Object to synchronize on instead. Write locks.put(key, new Object()), and keep the rest of your code the same.

huangapple
  • 本文由 发表于 2020年10月20日 02:59:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64433598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定