首页 \ 问答 \ 管理多个锁(Managing multiple locks)

管理多个锁(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
更新时间:2023-07-03 19:07

最满意答案

在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 the mochiweb.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 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.
  • 看起来你并没有将你的子模块推到你的主项目上。 只要推他们。 如果克隆应包含子模块的存储库,但在根级别没有.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跟踪文件夹不存在于s中,而是列出了类似链接的条目,其中包含像sha哈希的字母数字 那些不是子模块,而是简单的gitlink ( 索引中的特殊条目 ),代表嵌套的git repo SHA1。 请参阅“ 嵌套git repos和子模块之间的区别 ”。 有什么方法可以从我的主要git仓库中恢复我的更改,还是他们永远丢失了? 至少克隆你的repo(你推送gitlinks +你的代码的那个):你会在那里找到你的变化。 然后从一个新的回购开始,并强制推动。 有没有办法让内部仓库(现在是一个链接)到我将代码推送 ...
  • 在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 ...
  • 您可以将多个Git存储库添加到VSTS项目中。 您可以通过子模块推送引用另一个存储库的存储库。 在“代码”中心中,展开存储库,然后单击“ 新建存储库” : 使用第二个存储库存储子模块并从第一个存储库引用它。 子模块设置都在本地git存储库本地发生,使用您喜欢的任何工具或命令行,然后将存储库推送到VSTS。 You can add multiple Git repositories to your VSTS Project. And you can push a repository that referen ...
  • -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 ...
  • 将应用程序工具添加到项目的rebar.config的relx部分: {relx, [{release, {, "0.0.1"}, [,tools]}, {dev_mode, true}, {include_erts, false}, {extended_start_script, true}]}. tools是包含fprof模块的应用程序。 对于使用fprof模块, 这是来自erlang邮件列表的一 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。