同步可以被视为AOP中的一个方面吗

发布于 2024-12-07 06:57:42 字数 411 浏览 0 评论 0原文

我知道在 AOP 中,任何横切关注点(例如日志记录、事务等)都可以被视为一个方面,并且大多数 AOP 框架都很好地支持这些类型的横切关注点。

我的问题是,

  • 同步可以被视为横切关注点吗?
  • 如果是,是否有任何现有的库(包括 AspectJ 和 Spring AOP)支持此功能?

我搜索过但找不到很多例子。我遇到了一些受限制的研究论文(12)。

I understand that in AOP any cross cutting concerns such as Logging, transaction etc can be treated as an aspect and most of the AOP frameworks very well support these kind of cross-cutting concerns.

My question is,

  • Can Synchronization be treated as a crosscutting concern ?
  • If yes, are there any existing libraries (including AspectJ and Spring AOP) which support this functionality out of box ?

I searched but could not find many examples. I came across some restricted research papers (1,2) though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尘世孤行 2024-12-14 06:57:42

只是理论上的“答案”。 :)

据我了解 AOP,您可以向某些“切入点”/“联合点”添加独立行为/“建议”。但同步的目的是与其管理的代码紧密相关。

我想使用同步的方法是,如果它将作为建议附加,并将其自身提供为“联合点”,其他“方面”将定义一些“建议”。

或者,当您尝试调用一些“联合点”时,您可能会在“建议”中获得某种同步。

Just a theoretical "answer". :)

As I understand AOP, you add independent behaviours/"advices" to some "pointcuts"/"joint points". But synchronization is intended to be used tightly related with a code it manages.

I guess the way to use synchronization is if it will be attached as advice and will provide itself as a "joint point" for which other "aspects" will define some "advices".

Or you might get some kind of synchronization inside your "advices" while trying to call some "joint points".

谈下烟灰 2024-12-14 06:57:42

理论上,可以有一个 AOP 框架来向一组方法/类添加同步。实施起来并不困难。但这通常不是您想要的。太多的同步与太少的同步一样糟糕,因为您要么遇到死锁,要么对线程进行过多的排序,以至于无法有效地使用多个内核。

恐怕多线程编程没有不费脑子的捷径。

In theory it's possible to have an AOP framework which adds synchronization to a set of methods/classes. It's not even hard to implement. But it is usually not what you want. Too much synchronization is just as bad as too few synchronization, because you either run into deadlocks or you sequentialize your threading so much, that you are not able to use multiple cores effectively.

I am afraid there is no no brainer shortcut to multithreaded programming.

陈甜 2024-12-14 06:57:42

是的,同步可以被视为一个方面。 AOP 背后的想法不是为了处理横切问题吗?然后,可以通过 AOP 处理将同步视为横切问题,而无需定义和使用外部库。

考虑以下有关读写锁定的示例。每当一个对象需要读/写时,您就可以捕获该方法并为并发控制提供足够的功能。

public abstract aspect ReadWriteLockSynchronizationAspect 
    perthis(readOperations() || writeOperations()) {

    public abstract pointcut readOperations();

    public abstract pointcut writeOperations();

    private ReadWriteLock _lock = new SomeReadWriteLock();

    before() : readOperations() {
        _lock.readLock().acquire();
    }

    after() : readOperations() {
        _lock.readLock().release();
    }

    before() : writeOperations() {
        _lock.writeLock().acquire();
    }

    after() : writeOperations() {
        _lock.writeLock().release();
    }
}

perthis 为每个读/写操作创建一个新的方面。否则,只会创建一个方面,并且它的工作方式类似于单例对象。有关更多信息,请查看AspectJ 实际操作

Yes, synchronisation can be treated as an aspect. Isn't the idea behind AOP to handle cross cutting concerns? Then, regarding synchronisation as a cross cutting concern can be handled via AOP without defining and using external libraries.

Consider the following example about read-write locking. Whenever an object is subject to be read/write, then you can capture the method and provide sufficient functionality for concurrency control.

public abstract aspect ReadWriteLockSynchronizationAspect 
    perthis(readOperations() || writeOperations()) {

    public abstract pointcut readOperations();

    public abstract pointcut writeOperations();

    private ReadWriteLock _lock = new SomeReadWriteLock();

    before() : readOperations() {
        _lock.readLock().acquire();
    }

    after() : readOperations() {
        _lock.readLock().release();
    }

    before() : writeOperations() {
        _lock.writeLock().acquire();
    }

    after() : writeOperations() {
        _lock.writeLock().release();
    }
}

perthis creates a new aspect for each read/write operation. Otherwise, only one aspect will be created and it works like a singleton object. For further information check AspectJ in Action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文