简体   繁体   English

Java并发问题

[英]Java Concurrency Issue

I know that we can have a Map designed so that, using a concurrent utility, it allows map data to be modify by single user at one time.我知道我们可以设计一个地图,以便使用并发实用程序,它允许单个用户一次修改地图数据。 I want to know if this concurrent behaviour is only applicable for put operations or also for specifically the size operation.我想知道这种并发行为是否仅适用于put操作或还专门用于size操作。 The following code illustrates the question:下面的代码说明了这个问题:

One thread is in this block of the code一个线程在这个代码块中

if(map.size())
{
    //do something
}

while another thread is in this block而另一个线程在这个块中

map.put(obj);

I want to know if the size operation gets halted until the put operation completes and vice versa.我想知道size操作是否会在put操作完成之前停止,反之亦然。

In short, yes: size() must wait on a mutex before it can be performed.简而言之,是的: size()必须等待互斥锁才能执行。 The Collections.synchronizedMap(Map) returns a SynchronizedMap backed by the original map. Collections.synchronizedMap(Map)返回一个由原始地图支持的SynchronizedMap Every public method of the synchronized map are synchronized by a single private mutex.同步映射的每个公共方法都由一个私有互斥锁同步。 For example, here's the toString() method:例如,这里是toString()方法:

public String toString() {
    synchronized (mutex) {return m.toString();}
}

In order to guarantee serial access, it is critical that all access to the backing map is accomplished through the returned map.为了保证串行访问,对支持映射的所有访问都通过返回的映射完成是至关重要的。

Furthermore, manual synchronization may be necessary if you are, say iterating over the key set:此外,如果您需要手动同步,例如迭代密钥集:

It is imperative that the user manually synchronize on the returned * map when iterating over any of its collection views... Failure to follow this advice may result in non-deterministic behavior.当迭代任何集合视图时,用户必须在返回的 * 地图上手动同步...如果不遵循此建议可能会导致非确定性行为。

Consider the following example:考虑以下示例:

//inside thread1
if(map.size()>n){
    //do something
    System.out.println(map.size());
}

//inside thread2
map.put(obj);

In this case, the value of map.size() in the if statement of thread1 will not necessarily be the equal to the printed value of map.size() inside the //do something block.在这种情况下, map.size()if语句中map.size()的值不一定等于//do something块中map.size()的打印值。 In this case, you would have to manually synchronize on the map:在这种情况下,您必须在地图上手动同步:

synchronize(map){
    if(map.size()>n){
        //do something
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM