看哪个也不如看这个(Condition Objects)

本文详细介绍了条件变量的概念及其在多线程编程中的应用。条件变量通常与锁一起使用,以实现线程间的同步。文章解释了如何通过等待(wait)、通知(notify)及通知全部(notifyAll)等方法来协调线程活动,并提供了典型的生产者-消费者模型示例。

15.3.3 Condition Objects

A condition variable is always associated with some kind of lock; this can be passed in or one will be created by default. (Passing one in is useful when several condition variables must share the same lock.)

A condition variable has acquire() and release() methods that call the corresponding methods of the associated lock. It also has a wait() method, and notify() and notifyAll() methods. These three must only be called when the calling thread has acquired the lock.

The wait() method releases the lock, and then blocks until it is awakened by a notify() or notifyAll() call for the same condition variable in another thread. Once awakened, it re-acquires the lock and returns. It is also possible to specify a timeout.

The notify() method wakes up one of the threads waiting for the condition variable, if any are waiting. The notifyAll() method wakes up all threads waiting for the condition variable.

Note: the notify() and notifyAll() methods don't release the lock; this means that the thread or threads awakened will not return from their wait() call immediately, but only when the thread that called notify() or notifyAll() finally relinquishes ownership of the lock.

Tip: the typical programming style using condition variables uses the lock to synchronize access to some shared state; threads that are interested in a particular change of state call wait() repeatedly until they see the desired state, while threads that modify the state call notify() or notifyAll() when they change the state in such a way that it could possibly be a desired state for one of the waiters. For example, the following code is a generic producer-consumer situation with unlimited buffer capacity:

 

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()

To choose between notify() and notifyAll(), consider whether one state change can be interesting for only one or several waiting threads. E.g. in a typical producer-consumer situation, adding one item to the buffer only needs to wake up one consumer thread.

 

class Condition([lock])
If the lock argument is given and not None, it must be a Lock or RLock object, and it is used as the underlying lock. Otherwise, a new RLock object is created and used as the underlying lock.

 

acquire(*args)
Acquire the underlying lock. This method calls the corresponding method on the underlying lock; the return value is whatever that method returns.

 

release()
Release the underlying lock. This method calls the corresponding method on the underlying lock; there is no return value.

 

wait([timeout])
Wait until notified or until a timeout occurs. This must only be called when the calling thread has acquired the lock.

This method releases the underlying lock, and then blocks until it is awakened by a notify() or notifyAll() call for the same condition variable in another thread, or until the optional timeout occurs. Once awakened or timed out, it re-acquires the lock and returns.

When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof).

When the underlying lock is an RLock, it is not released using its release() method, since this may not actually unlock the lock when it was acquired multiple times recursively. Instead, an internal interface of the RLock class is used, which really unlocks it even when it has been recursively acquired several times. Another internal interface is then used to restore the recursion level when the lock is reacquired.

 

notify()
Wake up a thread waiting on this condition, if any. This must only be called when the calling thread has acquired the lock.

This method wakes up one of the threads waiting for the condition variable, if any are waiting; it is a no-op if no threads are waiting.

The current implementation wakes up exactly one thread, if any are waiting. However, it's not safe to rely on this behavior. A future, optimized implementation may occasionally wake up more than one thread.

Note: the awakened thread does not actually return from its wait() call until it can reacquire the lock. Since notify() does not release the lock, its caller should.

 

notifyAll()
Wake up all threads waiting on this condition. This method acts like notify(), but wakes up all waiting threads instead of one.
内容概要:本文提出了一种基于融合鱼鹰算法和柯西变异的改进麻雀优化算法(OCSSA),用于优化变分模态分解(VMD)的参数,进而结合卷积神经网络(CNN)与双向长短期记忆网络(BiLSTM)构建OCSSA-VMD-CNN-BILSTM模型,实现对轴承故障的高【轴承故障诊断】基于融合鱼鹰和柯西变异的麻雀优化算法OCSSA-VMD-CNN-BILSTM轴承诊断研究【西储大学数据】(Matlab代码实现)精度诊断。研究采用西储大学公开的轴承故障数据集进行实验验证,通过优化VMD的模态数和惩罚因子,有效提升了信号分解的准确性与稳定性,随后利用CNN提取故障特征,BiLSTM捕捉时间序列的深层依赖关系,最终实现故障类型的智能识别。该方法在提升故障诊断精度与鲁棒性方面表现出优越性能。; 适合人群:具备一定信号处理、机器学习基础,从事机械故障诊断、智能运维、工业大数据分析等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①解决传统VMD参数依赖人工经验选取的问题,实现参数自适应优化;②提升复杂工况下滚动轴承早期故障的识别准确率;③为智能制造与预测性维护提供可靠的技术支持。; 阅读建议:建议读者结合Matlab代码实现过程,深入理解OCSSA优化机制、VMD信号分解流程以及CNN-BiLSTM网络架构的设计逻辑,重点关注参数优化与故障分类的联动关系,并可通过更换数据集进一步验证模型泛化能力。
### 在 Spring 的 `@EventListener` 注解中编写多个条件判断的 condition 属性 在 Spring 中,`@EventListener` 注解的 `condition` 属性支持使用 SpEL(Spring Expression Language)表达式来定义复杂的条件逻辑。通过 SpEL 表达式,可以实现多个条件的组合判断。以下是一个详细的说明和示例。 #### 1. 使用逻辑运算符组合多个条件 SpEL 表达式支持逻辑运算符(如 `and`、`or` 和 `not`),可以将多个条件组合在一起。例如: ```java @EventListener(condition = "event.source != null and event.type == 'START'") public void handleStartEvent(MyCustomEvent event) { System.out.println("Handling start event with source: " + event.getSource()); } ``` 在此示例中,`condition` 属性中的表达式 `event.source != null and event.type == 'START'` 确保只有当 `event.source` 不为 `null` 且 `event.type` 等于 `'START'` 时才会触发监听器逻辑[^1]。 #### 2. 嵌套条件表达式 可以通过括号对条件进行分组,以实现更复杂的逻辑。例如: ```java @EventListener(condition = "(event.source != null and event.type == 'START') or event.priority > 5") public void handleComplexEvent(MyCustomEvent event) { System.out.println("Handling complex event with source: " + event.getSource()); } ``` 在此示例中,`condition` 属性中的表达式 `(event.source != null and event.type == 'START') or event.priority > 5` 确保满足以下任一条件时都会触发监听器逻辑: - `event.source` 不为 `null` 且 `event.type` 等于 `'START'` - `event.priority` 大于 `5`[^1] #### 3. 引用静态方法或常量 SpEL 表达式还可以引用静态方法或常量,从而增强条件表达式的灵活性。例如: ```java @EventListener(condition = "T(java.util.Objects).nonNull(event.source) and T(com.example.Constants).isValidType(event.type)") public void handleValidTypeEvent(MyCustomEvent event) { System.out.println("Handling valid type event with source: " + event.getSource()); } ``` 在此示例中,`T(java.util.Objects).nonNull(event.source)` 用于检查 `event.source` 是否不为 `null`,而 `T(com.example.Constants).isValidType(event.type)` 调用了一个静态方法来验证 `event.type` 的有效性[^1]。 #### 4. 示例代码整合 以下是一个完整的示例,展示了如何在 `@EventListener` 中使用多个条件判断的 `condition` 属性: ```java @SpringBootApplication public class EventApplication { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(EventApplication.class, args); context.publishEvent(new MyCustomEvent("Source1", "START", 3)); context.publishEvent(new MyCustomEvent(null, "STOP", 7)); context.publishEvent(new MyCustomEvent("Source2", "START", 6)); } } @Component public class EventListener { @EventListener(condition = "event.source != null and event.type == 'START'") public void handleStartEvent(MyCustomEvent event) { System.out.println("Handling start event with source: " + event.getSource()); } @EventListener(condition = "(event.source != null and event.type == 'START') or event.priority > 5") public void handleComplexEvent(MyCustomEvent event) { System.out.println("Handling complex event with source: " + event.getSource()); } @EventListener(condition = "T(java.util.Objects).nonNull(event.source) and T(com.example.Constants).isValidType(event.type)") public void handleValidTypeEvent(MyCustomEvent event) { System.out.println("Handling valid type event with source: " + event.getSource()); } } public class MyCustomEvent extends ApplicationEvent { private final String type; private final int priority; public MyCustomEvent(Object source, String type, int priority) { super(source); this.type = type; this.priority = priority; } public String getType() { return type; } public int getPriority() { return priority; } public Object getSource() { return super.getSource(); } } public class Constants { public static boolean isValidType(String type) { return "START".equals(type) || "PROCESS".equals(type); } } ``` #### 5. 注意事项 - 条件表达式的复杂度应适中,以免影响性能。 - 确保事件对象的属性命名清晰且易于理解,以便在条件表达式中正确引用。 - 如果条件逻辑过于复杂,建议将逻辑封装到事件类的方法中,并在条件表达式中调用这些方法。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值