Java ConcurrentHashMap损坏了值(Java ConcurrentHashMap corrupt values)
我有一个ConcurrentHashMap,有时表现出奇怪的行为。
当我的应用程序第一次启动时,我从文件系统中读取一个目录,并使用文件名作为键将每个文件的内容加载到ConcurrentHashMap中。 某些文件可能为空,在这种情况下,我将值设置为“空”。
一旦加载了所有文件,工作线程池将等待外部请求。 当请求进来时,我调用getData()函数,在那里我检查ConcurrentHashMap是否包含密钥。 如果密钥存在,我得到值并检查值是否为“空”。 如果value.contains(“empty”),我返回“找不到文件”。 否则,返回文件的内容。 当密钥不存在时,我尝试从文件系统加载该文件。
private String getData(String name) { String reply = null; if (map.containsKey(name)) { reply = map.get(name); } else { reply = getDataFromFileSystem(name); } if (reply != null && !reply.contains("empty")) { return reply; } return "file not found"; }
有时,ConcurrentHashMap将返回非空文件的内容(即
value.contains("empty") == false
),但是行:if (reply != null && !reply.contains("empty"))
返回FALSE。 我将IF语句分为两部分:
if (reply != null)
和if (!reply.contains("empty"))
。 IF语句的第一部分返回TRUE。 第二部分返回FALSE。 所以我决定打印出变量“reply”,以确定字符串的内容是否确实包含“empty”。 这不是这种情况,即内容不包含字符串“empty”。 此外,我添加了这条线int indexOf = reply.indexOf("empty");
由于变量回复在我打印出来时不包含字符串“empty”,因此我期望
indexOf
返回-1。 但是函数返回的值大约是字符串的长度,即if reply.length == 15100
,则reply.indexOf("empty")
返回15099。我每周都会遇到这个问题,每周大约2-3次。 此过程每天重新启动,因此会定期重新生成ConcurrentHashMap。
有没有人在使用Java的ConcurrentHashMap时看到过这样的行为?
编辑
private String getDataFromFileSystem(String name) { String contents = "empty"; try { File folder = new File(dir); File[] fileList = folder.listFiles(); for (int i = 0; i < fileList.length; i++) { if (fileList[i].isFile() && fileList[i].getName().contains(name)) { String fileName = fileList[i].getAbsolutePath(); FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(fileName); br = new BufferedReader(fr); String sCurrentLine; while ((sCurrentLine = br.readLine()) != null) { contents += sCurrentLine.trim(); } if (contents.equals("")) { contents = "empty"; } return contents; } catch (Exception e) { e.printStackTrace(); if (contents.equals("")) { contents = "empty"; } return contents; } finally { if (fr != null) { try { fr.close(); } catch (Exception e) { e.printStackTrace(); } } if (br != null) { try { br.close(); } catch (Exception e) { e.printStackTrace(); } } if (map.containsKey(name)) { map.remove(name); } map.put(name, contents); } } } } catch (Exception e) { e.printStackTrace(); if (contents.equals("")) { contents = "empty"; } return contents; } return contents; }
I have a ConcurrentHashMap that exhibits strange behavior on occasion.
When my app first starts up, I read a directory from the file system and load contents of each file into the ConcurrentHashMap using the filename as the key. Some files may be empty, in which case I set the value to "empty".
Once all files have been loaded, a pool of worker threads will wait for external requests. When a request comes in, I call the getData() function where I check if the ConcurrentHashMap contains the key. If the key exists I get the value and check if the value is "empty". If value.contains("empty"), I return "file not found". Otherwise, the contents of the file is returned. When the key does not exist, I try to load the file from the file system.
private String getData(String name) { String reply = null; if (map.containsKey(name)) { reply = map.get(name); } else { reply = getDataFromFileSystem(name); } if (reply != null && !reply.contains("empty")) { return reply; } return "file not found"; }
On occasion, the ConcurrentHashMap will return the contents of a non-empty file (i.e.
value.contains("empty") == false
), however the line:if (reply != null && !reply.contains("empty"))
returns FALSE. I broke down the IF statement into two parts:
if (reply != null)
andif (!reply.contains("empty"))
. The first part of the IF statement returns TRUE. The second part returns FALSE. So I decided to print out the variable "reply" in order to determine if the contents of the string does in fact contain "empty". This was NOT the case i.e. the contents did not contain the string "empty". Furthermore, I added the lineint indexOf = reply.indexOf("empty");
Since the variable reply did not contain the string "empty" when I printed it out, I was expecting
indexOf
to return -1. But the function returned a value approx the length of the string i.e.if reply.length == 15100
, thenreply.indexOf("empty")
was returning 15099.I experience this issue on a weekly basis, approx 2-3 times a week. This process is restarted on a daily basis therefore the ConcurrentHashMap is re-generated regularly.
Has anyone seen such behavior when using Java's ConcurrentHashMap?
EDIT
private String getDataFromFileSystem(String name) { String contents = "empty"; try { File folder = new File(dir); File[] fileList = folder.listFiles(); for (int i = 0; i < fileList.length; i++) { if (fileList[i].isFile() && fileList[i].getName().contains(name)) { String fileName = fileList[i].getAbsolutePath(); FileReader fr = null; BufferedReader br = null; try { fr = new FileReader(fileName); br = new BufferedReader(fr); String sCurrentLine; while ((sCurrentLine = br.readLine()) != null) { contents += sCurrentLine.trim(); } if (contents.equals("")) { contents = "empty"; } return contents; } catch (Exception e) { e.printStackTrace(); if (contents.equals("")) { contents = "empty"; } return contents; } finally { if (fr != null) { try { fr.close(); } catch (Exception e) { e.printStackTrace(); } } if (br != null) { try { br.close(); } catch (Exception e) { e.printStackTrace(); } } if (map.containsKey(name)) { map.remove(name); } map.put(name, contents); } } } } catch (Exception e) { e.printStackTrace(); if (contents.equals("")) { contents = "empty"; } return contents; } return contents; }
原文:https://stackoverflow.com/questions/11401197
最满意答案
你可以设置谷歌地图的样式。
以下示例将所有地图要素变为灰色,然后将动脉道路几何体以蓝色着色,并完全隐藏商业标签
var styleArray = [ { featureType: "all", stylers: [ { saturation: -80 } ] },{ featureType: "road.arterial", elementType: "geometry", stylers: [ { hue: "#00ffee" }, { saturation: 50 } ] },{ featureType: "poi.business", elementType: "labels", stylers: [ { visibility: "off" } ] } ];
有关详细信息,请参阅此链接
You can style google maps.
The following example turns all map features to gray, then colors arterial road geometry in blue, and hides business labels completely
var styleArray = [ { featureType: "all", stylers: [ { saturation: -80 } ] },{ featureType: "road.arterial", elementType: "geometry", stylers: [ { hue: "#00ffee" }, { saturation: 50 } ] },{ featureType: "poi.business", elementType: "labels", stylers: [ { visibility: "off" } ] } ];
Please refer this link for detailed info
相关问答
更多-
Google地图自定义HTML标记和MarkerClustererPlus(Google Maps Custom HTML Marker and MarkerClustererPlus)[2022-03-04]
如果您希望标记可见,则需要设置其map属性: for (var n = 0; n < this.markers.length; n++) { var marker = this.markers[n]; var distance = google.maps.geometry.spherical.computeDistanceBetween(marker.getPosition(), location) / 1609; if (distance > radius) { // 4 Marker ... -
确保包含Google Maps Javascript API(我假设您已经这样做了): JS: var map; var geocoder; function initialize() { var mapOptions = { center: new g ...
-
自定义谷歌地图无法正常工作(Custom google maps not working)[2023-11-11]
如果将display none设置为sidebar-wrapper元素,则可以看到它们。 您的地图宽度为100%,符号+和 - 位于侧边栏后面。 只是尝试调整这些元素的位置(例如,减小地图的大小),它会起作用。 If you set display none to the sidebar-wrapper element you can see them. Your map width is 100% and the symbols + and - are behind your sidebar. Just ... -
Google Maps API + Google Maps Engine /我的地图(Google Maps API + Google Maps Engine / My Maps)[2023-03-15]
您也可以下载KML等地图,下载网址类似 https://www.google.com/maps/d/kml?mid=themymaps.mapid 它没有记录,但是当我使用这个URL创建一个KmlLayer的时候它现在对我很有用(只要它们没有修改下载过程,它就会工作)。 示例( 原始地图 ) function initialize() { var map = new google.maps.Map(document.getElementById('map-canvas'), { zo ... -
HTML Google地图自定义(HTML Google Maps Customized)[2022-07-26]
你可以设置谷歌地图的样式。 以下示例将所有地图要素变为灰色,然后将动脉道路几何体以蓝色着色,并完全隐藏商业标签 var styleArray = [ { featureType: "all", stylers: [ { saturation: -80 } ] },{ featureType: "road.arterial", elementType: "geometry", stylers: [ { hue: "#00ffe ... -
我最终这样做的方法是在labelContent属性中添加丰富的HTML,通过labelClass在其上设置一个类,然后将icon设置为透明( https://maps.gstatic.com/mapfiles/transparent.png ) 这样,实际图标是不可见的,但您可以通过标签创建丰富的HTML / CSS标记。 这并没有破坏群集器的功能 The way I ended up doing this was to put in rich HTML into the labelContent prop ...
-
我不确定你想要加载到legend框中但是这可能有帮助吗? 您似乎希望使用与用户点击的event的数据更新legend 。 您应该能够在现有的listener执行此操作,如: document.getElementById('legend').textContent = 'Mag Type: ' + event.feature.getProperty('magType'); 希望这是有帮助的,但它可能完全取决于您的目标! 更新: 是的,您提供的图例是图像。 如果您愿意使用图像,只需将图像包含在图例div中, ...
-
瓷砖覆盖完全符合我的要求。 我不得不生成切片并覆盖tile url提供程序以从磁盘而不是url加载图像。 Tile overlays does exactly what I asked. I had to generate tiles and also override the tile url provider to load the images from disk instead of url.
-
我相信您需要与Google取得联系才能获得Maps API。 我不认为他们只是让街上的任何人拥有它。 http://support.google.com/enterprisehelp/bin/request.py?contact_type=maps_engine&hl=en I believe you will need to get in touch with Google to receive a Maps API. I don't think they just let anyone on the s ...
-
自定义Google地图类型(Custom Google Map Types)[2022-07-28]
通过使用GoogleMap.setMapType(MAP_TYPE_NONE),您将获得一张空地图。 使用我在这个SO问题的答案中描述的CanvasTileProvider,然后你可以在瓷砖中绘制任何东西。 By using GoogleMap.setMapType(MAP_TYPE_NONE) you will get an empty map. Using e.g. the CanvasTileProvider I have described in the answer to this SO ques ...