管理多个锁(Managing multiple locks)
我有以下情况:我同时处理具有给定密钥的请求。 只要每个正在进行的密钥都是唯一的,我就可以同时处理任意数量的请求。
我是Java中的并发新手。 必须有一些模式/实用/现有问题,但我无法弄清楚要搜索什么。 希望有人可以指出我正确的方向,或评论我到目前为止。
这个类管理锁:
class LockMap<K> { private Map<K, Object> locks = new HashMap<>(); void acquireLock(K key) throws InterruptedException { Object lockObj; synchronized (locks) { lockObj = locks.get(key); if (lockObj == null) lockObj = new Object(); locks.put(key, lockObj); } synchronized (lockObj) { lockObj.wait(); } } void releaseLock(K key) { Object lockObj; synchronized (locks) { lockObj = locks.get(key); locks.remove(key); } if (lockObj != null) { synchronized (lockObj) { lockObj.notify(); } } } }
然后我像这样使用锁管理器:
// lockMap is instance of LockMap shared across all threads void doSomething(K key) { lockMap.acquireLock(key); try { // something } finally { lockMap.releaseLock(key); } }
这是正确的方法吗?
I have the following situation: I'm concurrently processing requests that have a given key. I can process any number of requests at the same time, as long as each key in progress is unique.
I am a total rookie with concurrency in Java. There must be some pattern/utility/existing question for this, but I can't figure out what to search for. Hoping somebody could point me in the right direction, or comment on what I have so far.
This class manages the locks:
class LockMap<K> { private Map<K, Object> locks = new HashMap<>(); void acquireLock(K key) throws InterruptedException { Object lockObj; synchronized (locks) { lockObj = locks.get(key); if (lockObj == null) lockObj = new Object(); locks.put(key, lockObj); } synchronized (lockObj) { lockObj.wait(); } } void releaseLock(K key) { Object lockObj; synchronized (locks) { lockObj = locks.get(key); locks.remove(key); } if (lockObj != null) { synchronized (lockObj) { lockObj.notify(); } } } }
Then I use the lock manager like this:
// lockMap is instance of LockMap shared across all threads void doSomething(K key) { lockMap.acquireLock(key); try { // something } finally { lockMap.releaseLock(key); } }
Is this the right way to do it?
原文:https://stackoverflow.com/questions/21286831
最满意答案
在Erlang应用程序的代码中添加其他应用程序以允许其他开发人员轻松更新而不使用git子模块的正确方法是什么?
将该应用添加到rebar.config并使用:
./rebar update-deps
用于更新。 第一次,你需要使用:
./rebar get-deps
请参阅: https : //github.com/basho/rebar/wiki/Rebar-commands
现在,回到你的错误。
我的感觉是,您的代码中有一个(几乎)空的mochiweb目录,可能是因为使用了Git子模块。 当您运行
get-deps
命令时,钢筋默默地丢弃mochiweb,因为该目录已经存在。 但它期望一个OTP应用程序并查找mochiweb.app
文件,该文件不在那里(该目录为空)。 因此,错误。 如果我的解释是正确的,你可以简单地做:rm -rf deps/mochiweb ./rebar get-deps
不过,看看你的rebar.config会有所帮助。
What is the proper way of adding another app to the deps of an Erlang app to allow easy updates by the other developers without using git submodules?
Add the app to the rebar.config and use:
./rebar update-deps
For updating. The first time, you need to use:
./rebar get-deps
See: https://github.com/basho/rebar/wiki/Rebar-commands
Now, back to your error.
My feeling is that you have an (almost) empty directory for mochiweb in your deps, probably as a result of playing with Git submodules. When you run the
get-deps
command, rebar silently discards mochiweb, since the directory is already there. But it expects an OTP application and looks for themochiweb.app
file, which is not there (the directory is empty). Therefore, the error. If my interpretation is correct, you can simply do:rm -rf deps/mochiweb ./rebar get-deps
Having a look to your rebar.config would help, though.
相关问答
更多-
如何git标记所有子模块?(How to git tag all submodules?)[2023-11-06]
首先,确保您的子模块文件夹包含以下内容: git submodule update --init --recursive 然后,只需: git submodule foreach git tag -l 对于每个子模板,您应该看到tagName 。 意味着您之前的命令确实标记了这些子模块。 我建议制作带注释的标签 ,而不是轻量级 标签 : git submodule foreach git tag -m "tagName" tagName 这意味着您可以从每个子模块中推送该标签。 如果你只是在父级回购 ... -
如回顾性地添加 - 对git repo的反馈 git submodule update --init --recursive 应该工作 As mentioned in Retrospectively add --recursive to a git repo git submodule update --init --recursive should work.
-
Git子模块不拉(Git submodules not pulling)[2022-07-24]
看起来你并没有将你的子模块推到你的主项目上。 只要推他们。 如果克隆应包含子模块的存储库,但在根级别没有.gitmodules ,则意味着远程服务器没有注册它们。 首先确保子模块被推送到服务器。 您需要将子模块添加到父项目中(仅克隆或在另一个回购中创建回购是不够的)。 It seems that you didn't actually pushed your submodules on your master project. Just push them. If you clone a repositor ... -
如果somedir , someotherdir和thewanteddir已经是一个repo的一部分,那么你就不能直接看到thewanteddir (最好你会选择' theSubmoduleRootDir/thewanteddir ') ( 浅层克隆子模块不会只隔离一个子目录) 最干净的解决方案(但不是最简单的解决方案)是将子模块仓库拆分为两个 ,并使sourcesdir成为自己的仓库(然后可以将其添加为子模块) If somedir, someotherdir and thewanteddir are a ...
-
Git子模块混乱(Git submodules confusion)[2022-04-15]
内部git跟踪文件夹不存在于s中,而是列出了类似链接的条目,其中包含像sha哈希的字母数字 那些不是子模块,而是简单的gitlink ( 索引中的特殊条目 ),代表嵌套的git repo SHA1。 请参阅“ 嵌套git repos和子模块之间的区别 ”。 有什么方法可以从我的主要git仓库中恢复我的更改,还是他们永远丢失了? 至少克隆你的repo(你推送gitlinks +你的代码的那个):你会在那里找到你的变化。 然后从一个新的回购开始,并强制推动。 有没有办法让内部仓库(现在是一个链接)到我将代码推送 ... -
Git子模块和螺纹钢(Git submodules and rebar)[2023-02-04]
在Erlang应用程序的代码中添加其他应用程序以允许其他开发人员轻松更新而不使用git子模块的正确方法是什么? 将该应用添加到rebar.config并使用: ./rebar update-deps 用于更新。 第一次,你需要使用: ./rebar get-deps 请参阅: https : //github.com/basho/rebar/wiki/Rebar-commands 现在,回到你的错误。 我的感觉是,您的代码中有一个(几乎)空的mochiweb目录,可能是因为使用了Git子模块。 当您运行 ... -
尝试运行apt-get更新,然后在安装之前尝试自动完成以查看可用于安装的内容。 如果这不起作用,您可以直接从官方网站安装所需的软件包 Got it to work. I cloned rebar from github.com/rebar/rebar, checkedout branch 2.6.4, built the rebar, and coppied the resulting rebar script into /usr/bin directory. Now my rebar works fine ...
-
VSTS中的GIT子模块在线?(GIT Submodules in VSTS online?)[2022-05-03]
您可以将多个Git存储库添加到VSTS项目中。 您可以通过子模块推送引用另一个存储库的存储库。 在“代码”中心中,展开存储库,然后单击“ 新建存储库” : 使用第二个存储库存储子模块并从第一个存储库引用它。 子模块设置都在本地git存储库本地发生,使用您喜欢的任何工具或命令行,然后将存储库推送到VSTS。 You can add multiple Git repositories to your VSTS Project. And you can push a repository that referen ... -
Git子模块是-dirty?(Git submodules are being -dirty?)[2022-03-13]
-dirty后缀表示子模块已更改。 做这个: cd wp-content/plugins/advanced-custom-fields 然后 git status 并且您将看到哪些更改导致您的子模块变脏。 the -dirty suffix means the submodule has changed. do this: cd wp-content/plugins/advanced-custom-fields then git status and you'll see what changes ... -
使用具有钢筋的分析工具(Using profiling tools with rebar)[2023-01-28]
将应用程序工具添加到项目的rebar.config的relx部分: {relx, [{release, {, "0.0.1"}, [ ,tools]}, {dev_mode, true}, {include_erts, false}, {extended_start_script, true}]}. tools是包含fprof模块的应用程序。 对于使用fprof模块, 这是来自erlang邮件列表的一 ...