- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试减少分段数据的锁定对象的内存使用量。查看我的问题 here和 here 。或者假设您有一个字节数组,并且每 16 个字节都可以(反)序列化为一个对象。我们称其为行长度为 16 字节的“行”。现在,如果您从编写器线程修改这样的行并从多个线程读取,则需要锁定。如果字节数组大小为 1MB (1024*1024),这意味着 65536 行和相同数量的锁。
这有点太多了,而且我需要更大的字节数组,我想将其减少到与线程数大致成正比的程度。我的想法是创建一个
ConcurrentHashMap<Integer, LockHelper> concurrentMap;
哪里Integer
是行索引,在线程“进入”行之前,它会在此映射中放置一个锁对象(从 this answer 得到这个想法)。但无论我怎么想,我都找不到真正线程安全的方法:
// somewhere else where we need to write or read the row
LockHelper lock1 = new LockHelper();
LockHelper lock = concurrentMap.putIfAbsent(rowIndex, lock1);
lock.addWaitingThread(); // is too late
synchronized(lock) {
try {
// read or write row at rowIndex e.g. writing like
bytes[rowIndex/16] = 1;
bytes[rowIndex/16 + 1] = 2;
// ...
} finally {
if(lock.noThreadsWaiting())
concurrentMap.remove(rowIndex);
}
}
您认为有可能使其线程安全吗?
我有一种感觉,这看起来与concurrentMap.compute
非常相似。 construct (例如,参见 this answer )或者我什至可以利用这种方法吗?
map.compute(rowIndex, (key, value) -> {
if(value == null)
value = new Object();
synchronized (value) {
// access row
return value;
}
});
map.remove(rowIndex);
因为我们已经知道计算操作是原子的,所以值和“同步”是否有必要?
// null is forbidden so use the key also as the value to avoid creating additional objects
ConcurrentHashMap<Integer, Integer> map = ...;
// now the row access looks really simple:
map.compute(rowIndex, (key, value) -> {
// access row
return key;
});
map.remove(rowIndex);
顺便说一句:从什么时候起我们就用 Java 进行了这种计算。从1.8开始?在 JavaDocs 中找不到此内容
更新:我发现了一个非常相似的问题 here使用 userIds 而不是 rowIndices,请注意,该问题包含一个示例,其中存在多个问题,例如缺少 final
,调用 lock
里面try-finally
- 条款和缺乏缩小 map 。似乎还有a library JKeyLockManager为此目的,但是 I don't think it is thread-safe .
更新 2:解决方案似乎非常简单,Nicolas Filotto 指出了如何避免删除:
map.compute(rowIndex, (key, value) -> {
// access row
return null;
});
所以这确实是内存消耗较少的,但是使用 synchronized
进行简单的段锁定my scenario 至少快 50% .
最佳答案
Is the value and the
synchronized
necessary at all as we already know the compute operation is atomically?
我确认不需要添加synchronized
在本例中 block 为 compute
方法以原子方式完成,如 ConcurrentHashMap#compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
的 Javadoc 中所述。已添加 BiFunction
自 Java 8
,我引用:
Attempts to compute a mapping for the specified key and its current mapped value (or
null
if there is no current mapping). The entire method invocation is performed atomically. Some attempted update operations on this map by other threads may be blocked while computation is in progress, so the computation should be short and simple, and must not attempt to update any other mappings of thisMap
.
您试图通过 compute
实现什么目标如果你创建 BiFunction
,方法可能是完全原子的总是返回null
也以原子方式删除 key ,以便一切都将以原子方式完成。
map.compute(
rowIndex,
(key, value) -> {
// access row here
return null;
}
);
这样您就可以完全依赖 ConcurrentHashMap
的锁定机制。同步您对行的访问。
关于java - Java 中使用尽可能少的锁进行并发字节数组访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39675003/
我一直在使用 less 进行前端开发,但最近几天我遇到了这个错误。 我正在使用 PhpStorm 的观察器将 less 文件编译为 css 文件。但是当我编辑 less 文件时,编译器将这一行添加到
我在互联网上搜索Erlang的流程模型并找到了一些图表 slides 3-4在乔·阿姆斯特朗的一次演讲中。它们显示了 Erlang、java 和 C# 之间进程创建和消息传递时间之间的许多差异。谁能告
我怎样才能用更少的钱创建这个类? .class { display: none; } a:hover .class { display: block; } 最佳答案 像这样? .cla
全部,我有一些代码在 less 中做一个循环。但是如果我把px改成'%',less就不能编译less文件。我该怎么做呢?谢谢。 @iterations: 100; // helper class, w
According to the docs如果我做类似的事情: .child, .sibling { .parent & { color: black; } &
这是一个现有的通用 css 规则(原始文件): .caption-top { color: red; } 这是示意图,因为在现实生活中,我需要根据上下文将 .caption-top 选择器变成其他
所以问题是我想连接到msaccess 数据库,每次打开它时都有密码。 如果我直接打开 Access 文件,密码就有效。 如果我删除密码,我可以建立连接,这意味着如果不涉及密码,我的代码可以工作 密码是
news.less 看起来像这样; @import: "libs/base.less" base.less 看起来像这样; @import "colors.less"; @
当我在这里使用 WINLess 编译这段代码时出现错误: .icon-text-shadow (@icon-text-shadow: 0.0625rem 0.0625rem rgba(132, 108
我正在处理大型矩阵,例如 Movielens 20m dataset .我重组了在线文件,使其与页面上提到的尺寸(138000 x 27000)相匹配,因为原始文件包含的索引更大(138000 x 1
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我是 Android 新手,刚来这里。 我只知道 Bitmap 逐像素存储图像并且不进行任何压缩。 Drawable和Bitmap一样吗? 或者 同样的图片文件,Drawable 比 Bitmap 占
我是一名前端开发人员,最近考虑使用 SASS 或 LESS 进行 CSS 开发。 不过,我不使用 Ruby,也不想依赖于使用 JavaScript 的用户。有没有人对使用 PHP 项目使用 SASS
我需要将日历添加到表单中。 我想安装这个项目: https://github.com/vitalets/bootstrap-datepicker 但据说: 某些样式需要 Bootstrap 的下拉组件
如果您运行如下代码: length(unique(runif(10000000))) length(unique(rnorm(10000000))) 你会看到只有大约 99.8% 的 runif 值是
我正在这样做Question首先使用 PriorityQueue 解决了这个问题:- public ArrayList solve(int A, int B, int C, int D) {
基本上就是标题所说的。我知道如果我只有一个字母,我可以使用 char 作为类型,但我需要 2 个字母的数据类型,例如“XY”。有没有比字符串使用更少存储空间(位)或更小的东西?或者多个字母通常只是保存
我有两个表,用户表和程序表。现在我只有 5-10 个计划和数以万计的用户,他们可以注册任何一个计划(也可以注册多个计划)。因此,在多对多关系的情况下,我正在考虑创建一个单独的表,例如 link_use
我们有一个基于 LESS 的样式表,我们希望为其生成多种颜色变化。我们已经定义了一个包含颜色变化(现在为 blue.less)的包含文件,并希望生成和使用该包含文件的绿色和红色变化。 我们想要做的是通
我想知道我是否可以改进我的 LESS-Snippet。我有很多带有颜色名称的变量/我自己的颜色标题和相关的前景和背景颜色。我根据我的颜色定义类名称。 @logocolorgreen: #40FF01;
我是一名优秀的程序员,十分优秀!