- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
场景:
多线程从不同来源读取。
共享队列的一个访问点(请参阅尝试编写的 RiderSynchronized 类)
Reader 读取的每一行,都会尝试通过 RiderSynchronized 提供的方法插入到共享队列中。
当共享队列已满时,我必须对准备好的语句运行批处理以插入到 Oracle 中。同时,所有对共享队列的访问都必须被拒绝。
代码:
public class RiderSynchronized {
private ArrayDeque<JSONRecord> queue = new ArrayDeque<>();
private OracleDAO oracleDao;
private long capacity;
public RiderSynchronized(OracleDAO oracleDao, long capacity) {
this.oracleDao = oracleDao;
this.capacity = capacity;
}
public synchronized boolean addRecord(JSONRecord record) {
boolean success = false;
try {
while (queue.size() >= capacity) {
wait();
}
queue.add(record);
if (queue.size() < capacity) {
success = true;
notify(); //notify single Thread
} else {
JSONRecord currentRecord = null;
while ((currentRecord = queue.poll()) != null) {
oracleDao.insertRowParsedIntoBatch(currentRecord);
}
oracleDao.runBatch();
success = true;
notifyAll(); //it could be all Reading Threads are waiting. Notify all
}
} catch (Exception e) {
success = false;
}
return success;
}
}
我必须承认我有点担心一件事。
1) Reader线程可以模糊地使用addRecord吗?他们要等自己吗?或者我是否必须实现一些其他方法来检查运行 addRecord 方法之前的位置?
2)当queue.size <容量时,我决定只通知一个线程,因为恕我直言,此时,任何线程都不应该处于等待状态。我错了吗?我应该通知所有人吗?
2b) “else”语句的确切问题。通知所有是一个好习惯吗?此时,可能所有线程都在等待?
3)最后。我有点担心使用 Lock e Condition Classes 重写所有内容。这是一个更好的决定吗?或者我可以如何运行这个场景?
最佳答案
1) Reader threads can just use addRecord indistinctly? Are They going to wait for themselves? Or Do I have to implement some other method where to check before to run addRecord Method?
您当前代码的问题是,如果由于某种原因 notifyAll
不是由理论上应该能够进入 else
的唯一线程调用的阻塞然后你的线程将永远等待。
您的代码中的潜在风险是:
oracleDao.insertRowParsedIntoBatch(currentRecord)
oracleDao.runBatch()
如果这些方法之一抛出异常,则使用您当前的代码 notifyAll
永远不会被调用,所以你的线程将永远等待,你至少应该考虑调用 notifyAll
在 finally
阻止以确保它是否发生都会被调用。
2) When queue.size < capacity, I decide to notify just to one thread, because IMHO, at this point, no threads should be in status waiting. Am I wrong? Should I notify All?
您的线程只能等待以防万一 queue.size() >= capacity
所以对我来说notify
甚至不需要,因为任何线程都不需要这种条件( queue.size() < capacity
)。
2b) Exact question for the "else" statement. Is it a good practice to notifyAll? At this point, it could be all threds are waiting?
来自 Effective Java 的第 69 项:
<小时/>A related issue is whether you should use
notify
ornotifyAll
to wake waiting threads. (Recall thatnotify
wakes a single waiting thread, assuming such a thread exists, andnotifyAll
wakes all waiting threads.) It is often said that you should always usenotifyAll
. This is reasonable, conservative advice. It will always yield correct results because it guarantees that you’ll wake the threads that need to be awakened. You may wake some other threads, too, but this won’t affect the correctness of your program. These threads will check the condition for which they’re waiting and, finding it false, will continue waiting. As an optimization, you may choose to invokenotify
instead ofnotifyAll
if all threads that could be in the wait-set are waiting for the same condition and only one thread at a time can benefit from the condition becoming true. Even if these conditions appear true, there may be cause to usenotifyAll
in place of notify. Just as placing the wait invocation in a loop protects against accidental or malicious notifications on a publicly accessible object, usingnotifyAll
in place ofnotify
protects against accidental or malicious waits by an unrelated thread. Such waits could otherwise “swallow” a critical notification, leaving its intended recipient waiting indefinitely.
3) Finally. I'm a little concerned to re-write everything using Lock e Condition Classes. Is it a better decision? Or Is it ok how I'm running this scenario?
Lock
和Condition
如果您需要内在锁无法提供的功能(例如 tryLock()
),那么这些功能很有趣。或者仅唤醒等待给定条件的线程的能力。就您而言,这似乎没有必要,因此您可以保持原样。
关于java - 访问共享资源,锁定解锁或等待通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40512351/
我注意到,当我打开我的应用程序并等待足够长的时间让我的手机自行锁定时,当我解锁它时,当前内容 View 未处于全屏模式。它不显示顶部栏,而是全黑线...如果我去另一个 Activity 并返回它再次修
我必须处理设计限制(依赖性问题)以不对互斥体使用范围保护。 示例代码: template class Test { MutexType myMutex; public: void f(
我正在尝试像这样使 my_class 线程安全。 class my_class { const std::vector& get_data() const { //lock so that
我想让手机在手机被锁定(锁定 = 键盘被锁定)并且用户想要解锁时询问一个问题(来自数据库)。如果答案正确,则手机解锁,否则手机会询问另一个问题。 我不知道如何检测解锁并在需要时解锁/锁定手机。 请帮帮
我有一个对 accept() 的阻塞调用。我从另一个线程关闭套接字,希望它能解除对 accept() 调用的阻塞,它确实如此,但我有一个情况,它没有:例如线程A进入accept(),线程B关闭sock
我在 C# 中有一个可以从多个线程调用多次的函数,我希望它只执行一次,所以我想到了这个: class MyClass { bool done = false; public void
锁定数据库的一个表 复制代码 代码如下: SELECT * FROM table WITH (HOLDLOCK) 注意: 锁定数据库的一个表的区别
我已经使用腻子在我的 ec2 实例上安装了 jenkins,从我的 Windows 系统 当我尝试通过网络访问 jenkins 时,我使用/var/lib/jenkins/secrets/initia
每次我打开我的电脑并尝试连接到 HR 数据库时,都会弹出一个错误提示被阻止,然后我转到 SQLPLUS 并以 SYSDBA 身份登录,并执行以下脚本: 我的问题是,我是否总是必须这样做?有什么办法让它
所以我的手机在引导加载程序中,我想解锁它 所以我输入 fastboot oem unlock 我明白了 D:\Minimal ADB and Fastboot>fastboot oem unlock
在我的应用程序中,我希望即使在应用程序终止后我的应用程序仍处于事件状态。我希望当“滑动解锁”我的应用程序启动时。 有可能吗?我怎样才能在不越狱的情况下做到这一点...... 这是为用户提供便利的功能。
即使我的 iPhone 使用电缆连接并解锁,它也会显示警告“Xcode 9.3 无法在 iPhone 上启动应用程序,因为设备已锁定”。我不知道该怎么办?有人对此有适当的解决方案吗?请 最佳答案 只需
我看到一个非常奇怪的行为,我试图了解它是否是这样设计的,或者只是没有从我的应用程序中正确处理。 我的应用程序以 UIAlertViews 的形式发送推送通知。当设备未锁定时,UIAlertViews
该文档说,从一个线程进行锁定并从另一个rwlock进行解锁会导致未定义的行为。我有一个数组和两个线程,一个分配它,一个释放它,这是一个周期发生的,也有一些线程在其中进行读/写操作,但是它们从不重叠,因
解锁 Oracle 表有哪些不同的技术? 我尝试过的。 获取锁定表的对象ID: SELECT object_id FROM dba_objects WHERE object_name='YOUR TA
我运行@apxchpwd.sql并用这个脚本给帐户倒霉 alter session set current_schema = APEX_050100; declare l_workspace_id
我想在 Chrome 扩展程序中提供一些高级功能。这个想法是,只要用户通过 PayPal 捐赠了一定金额,就可以使用后者的附加功能。 目前我的策略是这样的: 1/。用户填写 PayPal 表格。 2/
我构建了一个包含 IAP(应用内购买)的应用。该应用程序是使用 UINavigationController 构建的。当用户决定购买内容时,解锁功能会解锁该内容。然后,用户点击后退按钮,他们将返回主菜
我有一个只能通过静态方法从外部访问的类。然后,这些静态方法创建该类的一个对象以在该方法中使用,然后它们返回并且该对象可能被销毁。该类是几个配置文件的 getter/setter,现在我需要在对配置文件
我可以在超出 scoped_lock 范围之前解锁互斥锁吗?我怎么能那样做? {boost::mutex::scoped_lock lock(mutex); if(conditionaA) {
我是一名优秀的程序员,十分优秀!