离开同步块时自动notify()/notifyAll() [英] Automatic notify()/notifyAll() on leaving a synchronized block

查看:132
本文介绍了离开同步块时自动notify()/notifyAll()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在考虑向Java语言架构师发送建议.

I have been thinking of send a proposal to the Java language architects.

在同步块中

synchronized(lock) {

// If there is no notification before this point
// <--- implicitly put here // lock.notifyAll(); // OR // lock.notify();  
}

在线程离开同步块之后,它不能在没有异常的情况下再次调用lock.notifyAll()/lock.notify().

After a thread left synchronized block, it cannot call lock.notifyAll() / lock.notify() anymore without getting exception.

忘记通知其他线程监视器持有者可能永远使他们(其他线程)等待(除非他们在其wait方法中设置了超时).

Forgetting to notify other thread monitor holders may forever make them (other threads) wait (unless, they put some timeout in their wait method).

synchronized(lock) {

     lock.wait(); //<--- this thread may forever freeze here
}

我无法想象这种情况(在没有显式通知的情况下在同步块的末尾插入隐式通知)是不受欢迎的.

I cannot imagine a situation in which such behavior (inserting implicit notification at the end of a synchronized block, when there is no explicit notification) is undesirable.

相同的方法可以应用于同步方法.

The same approach can be applied to synchronized methods.

可以(技术上)实现这种行为的方式有多种,例如:

There can be different ways how to (technically) implement such behavior, for example:

@autonotify
synchronized(lock) {
...
}

@autonotify
public void synchronized doSomething() {
...
}

或者:

@autonotifyAll
synchronized(lock) {
...
}

@autonotifyAll
public void synchronized doSomething() {
...
}

或者-将自动通知设置为默认行为,但保留禁止行为的功能,例如:

Or - make auto-notification the default behavior, but leaving ability to suppress it, for example:

@suppressautonotify
synchronized(lock) {
...
}

@suppressautonotifyAll
public void synchronized doSomething() {
...
}


您怎么看?异议?


What do you think? Objections?

支持或反对提案的最佳评论将被接受.

The best commentary for or against the proposal will be accepted as the answer.

推荐答案

自动执行或默认情况下执行此操作是一个很大的禁忌.在许多情况下,您不希望在已同步块的末尾进行通知而在锁上进行同步.这样做会破坏很多现有程序.

Doing it automatically or by default is a big no-no. There are many situations where you synchronize on a lock without wanting to notify at the end of the synchronized block. Doing so would break a whole lot of existing programs.

为什么在同步块的末尾使用@autonotifyAll而不是简单的lock.notifyAll()来执行此操作.如果您忘记拨打lock.notifyAll(),则有很多机会忘记@autonotifyAll.这样会使内容的可读性和一致性降低.

And why do it with @autonotifyAll instead of doing it with a simple lock.notifyAll() at the end of the synchronized block. If you forget to call lock.notifyAll(), you have as many chances to forget @autonotifyAll. And it would make things less readable, and less consistent.

无论如何,最佳实践是避免使用这些非常低级的方法,而要使用更高级别的抽象,例如阻塞队列,倒数锁存器,信号灯等.

The best practice, anyway, is to avoid using these very low-level methods, and to use higher-level abstractions, like blocking queues, countdown latches, semaphores, etc.

如果我必须决定,您的建议将被拒绝.

If I had to decide, your suggestion would be rejected.

这篇关于离开同步块时自动notify()/notifyAll()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆