自定义阻塞队列锁定问题(Custom blocking queue locking issue)
我试图用固定长度的字节数组数组做一些自定义的阻塞队列实现。 我没有删除轮询元素,因此我调整了put方法以返回字节数组,以便可以直接写入(生成器线程使用MappedByteBuffer直接写入此字节数组)。 我添加了“commitPut()”方法来简单地增加计数器并设置“长度”数组。 (如果多个线程正在编写,这可能是并发问题,但我知道只有一个线程在写)。
以下是我目前的情况。 它是有效的,如果我一步一步调试,但如果我“运行”它看起来像遇到一些锁定问题。 我复制,剥离并调整了ArrayBlockingQueue代码。 有更好的知识的人可以看看课程并告诉我我做错了什么,或者如何做得更好(比如直接写入缓冲区并在同一步骤设置长度数组和计数器)?
public class ByteArrayBlockingQueue { private final int[] lens; // array to valid lengths private final byte[][] items; // array of byte arrays private int takeIndex = 0; private int putIndex = 0; private int count = 0; public volatile int polledLen = 0; // lenght of last polled byte array private final ReentrantLock lock; private final Condition notEmpty; private final Condition notFull; final int inc(int i) { return (++i == items.length)? 0 : i; } public ByteArrayBlockingQueue(int capacity, int size, boolean fair) { if (capacity <= 0) throw new IllegalArgumentException(); this.items = new byte[capacity][size]; this.lens = new int[capacity]; lock = new ReentrantLock(fair); notEmpty = lock.newCondition(); notFull = lock.newCondition(); } public byte[] put() throws InterruptedException { final byte[][] items = this.items; final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { try { while (count == items.length) notFull.await(); } catch (InterruptedException ie) { notFull.signal(); // propagate to non-interrupted thread throw ie; } //insert(e, len); return items[putIndex]; } finally { lock.unlock(); } } public void commitPut(int lenBuf) throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { lens[putIndex] = lenBuf; putIndex = inc(putIndex); ++count; notEmpty.signal(); } finally { lock.unlock(); } } public byte[] poll() { final ReentrantLock lock = this.lock; lock.lock(); try { if (count == 0) return null; final byte[][] items = this.items; final int[] lens = this.lens; byte[] e = items[takeIndex]; this.polledLen = lens[takeIndex]; //items[takeIndex] = null; takeIndex = inc(takeIndex); --count; notFull.signal(); return e; } finally { lock.unlock(); } } }
I am trying to do some custom implementation of blocking queue with fixed length array of byte arrays. I am not removing polled elements, therefore I adjusted put method to return byte array so that it can be written directly (producer thread uses MappedByteBuffer to write directly to this byte array). I added "commitPut()" method to simply increase counters and set "lengths" arrays. (if multiple threads would be writing this could be the concurrency problems, but I know that only one thread is writing).
Below is what I currently have. It works if I debug through step by step, but if I "run" it looks like it encounters some locking problems. I copied, stripped down and adjusted ArrayBlockingQueue code. Can someone with better knowledge please look at the class and tell me what I am doing wrong, or how to do it better (like write directy to buffer and set lengths array and counters at the same step)?
public class ByteArrayBlockingQueue { private final int[] lens; // array to valid lengths private final byte[][] items; // array of byte arrays private int takeIndex = 0; private int putIndex = 0; private int count = 0; public volatile int polledLen = 0; // lenght of last polled byte array private final ReentrantLock lock; private final Condition notEmpty; private final Condition notFull; final int inc(int i) { return (++i == items.length)? 0 : i; } public ByteArrayBlockingQueue(int capacity, int size, boolean fair) { if (capacity <= 0) throw new IllegalArgumentException(); this.items = new byte[capacity][size]; this.lens = new int[capacity]; lock = new ReentrantLock(fair); notEmpty = lock.newCondition(); notFull = lock.newCondition(); } public byte[] put() throws InterruptedException { final byte[][] items = this.items; final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { try { while (count == items.length) notFull.await(); } catch (InterruptedException ie) { notFull.signal(); // propagate to non-interrupted thread throw ie; } //insert(e, len); return items[putIndex]; } finally { lock.unlock(); } } public void commitPut(int lenBuf) throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { lens[putIndex] = lenBuf; putIndex = inc(putIndex); ++count; notEmpty.signal(); } finally { lock.unlock(); } } public byte[] poll() { final ReentrantLock lock = this.lock; lock.lock(); try { if (count == 0) return null; final byte[][] items = this.items; final int[] lens = this.lens; byte[] e = items[takeIndex]; this.polledLen = lens[takeIndex]; //items[takeIndex] = null; takeIndex = inc(takeIndex); --count; notFull.signal(); return e; } finally { lock.unlock(); } } }
原文:https://stackoverflow.com/questions/16637516
最满意答案
回答很简单:
"/bin/bash", "-c", " cd " + path.toString() + " && cordova platform add " + os
如果忘了在cd-cmd之前添加一个空字符并将所有内容放在一行中。
The answere is fairly simple:
"/bin/bash", "-c", " cd " + path.toString() + " && cordova platform add " + os
If forgot to add an empty char before the cd-cmd and put everything in one line.
相关问答
更多-
我对此做了一些分析,看起来像它与android SDK兼容性的问题。 检查Cordova在C:/Users/%USER%/.cordova/lib/npm-cache/cordova-android (如果是Windows操作系统)下提供的Android版本,并安装相应的android平台版本。 例如:cordova平台添加android@6.0.0 希望能帮助到你。 干杯 更新:我猜问题-CB-12767应该可以解决你的问题。 --nofetch是关键 I did some analysis on thi ...
-
尝试使用--nofetch进行安装,而不使用新的npm安装机制: cordova plugin add path:\to\my-plugin --nofetch 请参阅https://cordova.apache.org/news/2017/05/04/cordova-7.html Try using --nofetch to install without using the new npm install mechanism: cordova plugin add path:\to\my-plugin ...
-
首先,您应该列出您的插件: cordova plugin list 有了这个结果,你可以简单地做: cordova plugin remove
例如: cordova plugin remove org.apache.cordova.media 希望有帮助 First, you should list your plugins: cordova plugin list With this result, you can simply do: cordova plugin ... -
在使用“cordova build”构建应用程序后,我在IntelliJ中导入项目的步骤: 导入项目 选择“PROJECT_FOLDER / platforms” 从外部模型“Eclipse”导入项目 在选择的Eclipse项目中,选择all。 导入JDK和SDK(在我的案例中为JDK 1.7和SDK Android API 17和19) 从属性文件导入Android依赖项(添加依赖项helloworls - > helloworld-cordovaLib)确定。 最后这个构建并运行没有问题给我。 The ...
-
phonegap / cordova创建项目说缺少以下之一(phonegap/cordova create project says missing one of following)[2022-07-21]
除了在PATH系统变量中添加JDK和Apache Ant之外,还要添加两个系统变量:JAVA_HOME(C:\ Program Files \ Java \ jdk1.7.0_10)和ANT_HOME(D:\ Development \ apache-ant-1.8.4) 。 Aside from adding JDK and Apache Ant in your PATH system variable, add two more system variables: JAVA_HOME (C:\Progr ... -
cordova prepare更新www文件夹。 进一步来说 : prepare [platform ...]将文件复制到指定的平台或所有平台。 然后它就可以通过Eclipse,Xcode等进行构建。 compile [platform ...]将应用程序编译为每个目标平台的二进制文件。 没有参数,为所有平台构建,否则为指定平台构建。 构建 [platform ...] cordova准备的别名,然后是cordova编译。 ref: https : //github.com/apache/cordova-c ...
-
解决了它:Cmd看起来像这样“cordova create / path / to / generated / app / Hello World” 请注意,在调用cmd之前必须存在路径 Resolved it: Cmd looks like this "cordova create /path/to/generated/app/ Hello World" Be aware, the path has to exist before the cmd is beeing called
-
回答很简单: "/bin/bash", "-c", " cd " + path.toString() + " && cordova platform add " + os 如果忘了在cd-cmd之前添加一个空字符并将所有内容放在一行中。 The answere is fairly simple: "/bin/bash", "-c", " cd " + path.toString() + " && cordova platform add " + os If forgot to add an em ...
-
在我的〜/ .bash_profile中我有以下几组: #export ANDROID_HOME="/Users/idanadar/Documents/MobileFirst/Setup/Android/" #export ANDROID_ROOT="/Users/idanadar/Documents/MobileFirst/Setup/Android/platform-tools/" 然后在终端我做了: mfp cordova create ,按照交互式说明,添加了Android环境并且能够成功创建它。 ...
-
Cordova 4.1.2 CLI Android Build Fail Windows(Cordova 4.1.2 CLI Android Build Fail Windows)[2023-04-07]
尝试删除此文件: %USERPROFILE%/.android/debug.keystore 或者在Linux / OS X上: ~/.android/debug.keystore 这是您的调试证书,似乎已过期。 Try to delete this file: %USERPROFILE%/.android/debug.keystore or on Linux/OS X: ~/.android/debug.keystore It's your debug certificate, which s ...