@StateMachine static interface LockingStateMachine { @StateSet static interface States { @Initial @Function(transition = LockingStateMachine.Transitions.Start.class, value = Started.class) static interface Created {} @Functions({ @Function(transition = LockingStateMachine.Transitions.Stop.class, value = Stopped.class), @Function(transition = LockingStateMachine.Transitions.Cancel.class, value = Canceled.class) }) static interface Started {} @End static interface Stopped {} @End static interface Canceled {} } @TransitionSet static interface Transitions { static interface Start {} static interface Stop {} static interface Cancel {} } } static interface ILockingReactiveObject { public abstract int getCounter(); public abstract void start(); public abstract void stop(); public abstract void cancel(); }
public static class SimpleLock implements LifecycleLockStrategry { private static Logger logger = Logger.getLogger("Lifecycle Framework"); private static volatile int depth = 0; private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); @Override public void lockRead(Object reactiveObject) { logLockingMethod(reactiveObject, "lockRead: "); lock.readLock().lock(); depth++; } private void logLockingMethod(Object reactiveObject, String methodName) { final StringBuilder builder = getIndent(); builder.append(methodName + reactiveObject); logger.fine(builder.toString()); } private StringBuilder getIndent() { StringBuilder builder = new StringBuilder(); for ( int i = 0; i klass) throws Throwable { final ExecutorService executorService = Executors.newFixedThreadPool(7); for ( int i = 0; i c1 = new Callable() { @Override public LifecycleException call() throws Exception { try { object.stop(); return null; } catch (LifecycleException e) { return e; } } }; Callable c2 = new Callable() { @Override public LifecycleException call() throws Exception { try { object.cancel(); return null; } catch (LifecycleException e) { return e; } } }; if ( i % 2 == 0 ) { Callable temp = c1; c1 = c2; c2 = temp; } final Future f1 = executorService.submit(c1); final Future f2 = executorService.submit(c2); final Callable c3 = new Callable() { @Override public Exception call() throws Exception { try { final LifecycleException e1 = f1.get(); final LifecycleException e2 = f2.get(); assertFalse(( null != e1 && null != e2 ) || ( null == e1 && null == e2 )); final LifecycleException e = null != e1 ? e1 : e2; System.out.println(e.toString()); assertEquals(LifecycleCommonErrors.ILLEGAL_TRANSITION_ON_STATE, e.getErrorCode()); assertEquals(2, object.getCounter()); return null; } catch (Exception e) { return e; } } }; final Future f3 = executorService.submit(c3); if ( null != f3.get() ) { fail(f3.get().getMessage()); } } }
日志截屏如下(请点击放大)
日志解释:
执行过程中的加(获取)写锁和解(释放)写锁的过程
[FINE]: Found Intercept Point: class net.madz.lifecycle.engine.LifecycleLockTestMetadata$SimpleLockingReactiveObject.start( )
[FINE]: Intercepting....instatiating InterceptContext ...
[FINE]: Intercepting....InterceptorController is doing exec ...
[FINE]: Intercepting....instantiating LifecycleInterceptor
[FINE]: intercepting with :net.madz.bcel.intercept.LifecycleInterceptor @preExec
[FINE]: lockWrite: net.madz.lifecycle.engine.LifecycleLockTestMetadata$SimpleLockingReactiveObject@62b9f149
[FINE]: intercepting [net.madz.lifecycle.engine.LifecycleLockTestMetadata$SimpleLockingReactiveObject@62b9f149]
from state: [Created]
[FINE]: Step 1. start validating State [Created]
[FINE]: Step 2. start validating transition: [Start] on state: [Created]
[FINE]: Step 3. start validating inbound relation constraint is next state is predictable before method invocation.
[FINE]: Step 4. start callback before state change from : Created => to : Started
[FINE]: intercepting with: net.madz.bcel.intercept.CallableInterceptor @intercept
[FINE]: intercepting with :net.madz.bcel.intercept.LifecycleInterceptor @postExec
[FINE]: Step 5. start validating inbound relation constraint is next state after method invocation.
[FINE]: Step 6. Set next state to reactiveObject.
[FINE]: Step 6. ReactiveObject is tranisited to state: [Started]
[FINE]: Step 7. Start Callback after state change from : Created => to : Started
[FINE]: unlockWrite: net.madz.lifecycle.engine.LifecycleLockTestMetadata$SimpleLockingReactiveObject@62b9f149
[FINE]: Step 8. Start fire state change event.
[FINE]: intercepting with :net.madz.bcel.intercept.LifecycleInterceptor @cleanup
[FINE]: Intercepting....LifecycleInterceptor is doing cleanup ...
并发执行同步过程日志
//线程一开始获取写锁
[FINE]: lockWrite: net.madz.lifecycle.engine.LifecycleLockTestMetadata$SimpleLockingReactiveObject@31c1fb39
//线程二开始获取写锁
[FINE]: lockWrite: net.madz.lifecycle.engine.LifecycleLockTestMetadata$SimpleLockingReactiveObject@31c1fb39
//线程一得到写锁并继续执行生命周期引擎
[FINE]: intercepting [net.madz.lifecycle.engine.LifecycleLockTestMetadata$SimpleLockingReactiveObject@31c1fb39]
from state: [Started]
[FINE]: Step 1. start validating State [Started]
[FINE]: Step 2. start validating transition: [Stop] on state: [Started]
[FINE]: Step 3. start validating inbound relation constraint is next state is predictable before method invocation.
[FINE]: Step 4. start callback before state change from : Started => to : Stopped
[FINE]: intercepting with: net.madz.bcel.intercept.CallableInterceptor @intercept
[FINE]: intercepting with :net.madz.bcel.intercept.LifecycleInterceptor @postExec
[FINE]: Step 5. start validating inbound relation constraint is next state after method invocation.
[FINE]: Step 6. Set next state to reactiveObject.
[FINE]: Step 6. ReactiveObject is tranisited to state: [Stopped]
[FINE]: Step 7. Start Callback after state change from : Started => to : Stopped
//第一线程执行完Transition开始解写锁
[FINE]: unlockWrite: net.madz.lifecycle.engine.LifecycleLockTestMetadata$SimpleLockingReactiveObject@31c1fb39
[FINE]: Step 8. Start fire state change event.
//线程二得到写锁开始执行引擎
[FINE]: intercepting [net.madz.lifecycle.engine.LifecycleLockTestMetadata$SimpleLockingReactiveObject@31c1fb39]
from state: [Stopped] //此时状态已经变为Stopped,线程一导致的变化至此已被线程二看见
[FINE]: intercepting with :net.madz.bcel.intercept.LifecycleInterceptor @cleanup
[FINE]: Step 1. start validating State [Stopped]
[FINE]: Intercepting....LifecycleInterceptor is doing cleanup ...
[FINE]: Step 2. start validating transition: [Cancel] on state: [Stopped]
[FINE]: intercepting with :net.madz.bcel.intercept.LifecycleInterceptor @cleanup
[FINE]: Intercepting....LifecycleInterceptor is doing cleanup ...
[SEVERE]: ReactiveObject: [net.madz.lifecycle.engine.LifecycleLockTestMetadata$SimpleLockingReactiveObject@31c1fb39] was failed to transit from state: [Stopped] to state: [(Had Not Been Evaluated)] with following error:
//由于在执行过程中另一线程导致状态发生变化,而使得本线程的Transition不合法
[SEVERE]: Illegal Request Found on object net.madz.lifecycle.engine.LifecycleLockTestMetadata$SimpleLockingReactiveObject@31c1fb39: Transition Cancel is not allowed while object is on Stopped state.
//线程二Transition失败后开始释放写[FINE]: unlockWrite: net.madz.lifecycle.engine.LifecycleLockTestMetadata$SimpleLockingReactiveObject@31c1fb39
前文:生命周期组件框架:关系型状态机服务
相关推荐
### 并发编程——并发工具类 #### 知识点概述 并发编程是Java编程中的高级主题,主要目的是为了让程序能够充分利用多核CPU的优势,提高程序的执行效率。并发工具类则是Java中用于支持多线程协作和同步控制的一系列...
数据库思维导图——并发控制 并发控制 多事务执行方式 (1)事务串行执行 每个时刻只有一个事务运行,其他事务必须等到这个事务结束以后方能运行 不能充分利用系统资源,发挥数据库共享资源的特点 (2)交叉并发方式...
在 Laravel 框架中,分布式并发锁是一个关键的概念,特别是在高并发的场景下,它能够确保多个请求在处理同一资源时不会发生冲突。这里,我们深入探讨 Laravel 的并发锁机制,以及如何在 PHP 开发中利用这个特性。 ...
Skynet是一个开源并发框架,它为了简悦的第一个MMORPG服务器所编写,但应用领域不限于网络游戏。Skynet的核心部分不到3000行C代码,利用actor模式充分利用单机多核的优势,尽量将业务逻辑进行并行处理。相对于Erlang...
标题 "Tomcat 生命周期与事件管理——LifeCycle & Event" 涉及到的是Apache Tomcat服务器的核心运行机制。Tomcat是Java Servlet和JavaServer Pages(JSP)技术的开源Web应用服务器,它遵循Java EE规范,是开发和部署...
C#中的`ThreadPool`类提供线程池服务,通过`QueueUserWorkItem`方法提交任务到线程池,适合执行大量短生命周期的任务。 七、异常处理与死锁防范 在并发环境中,异常处理和死锁预防尤为重要。C#提供了try-catch-...
《iOS组件与框架——iOS SDK高级特性剖析》的源码包包含了丰富的开发示例,旨在帮助开发者深入理解iOS系统的高级特性。在这个压缩包中,你可以找到22个章节的源代码,涵盖了书中大部分内容,遗憾的是,第11章、14...
这一技术专注于文档从创建到最终处置的整个过程,包括生产、存储、审核、发布等环节,确保文档在整个生命周期中得到有效的管理和控制。下面将深入探讨文档生命周期管理的关键概念、重要性和实施策略。 ### 文档生命...
《分布式对象存储——原理 架构及Go语言实现》这本书可能涵盖了以下几个方面的内容: 1. 分布式系统基础:首先,书籍可能会介绍分布式系统的概念,包括其基本特征、优势以及面临的挑战,如数据一致性、容错性、网络...
SSM实战项目——Java高并发秒杀API是一个典型的Java Web开发案例,主要涉及Spring、SpringMVC和MyBatis这三大框架的整合应用。在这个项目中,开发者将学习如何构建一个能够处理高并发请求的秒杀系统,以满足电商或...
9. **生命周期方法**:在某些编程语言和框架中,领域对象可能包含一些生命周期方法,如 `save()`、`delete()`、`load()` 等,用于操作对象的生命周期。 10. **CQRS(命令查询责任分离)**:在复杂系统中,CQRS 模式...
- **减少锁持有时间**:尽量缩短锁的生命周期,降低并发冲突的可能性。 - **减小锁粒度**:如ConcurrentHashMap的分段锁,提高并行度。 - **锁分离**:如ReadWriteLock,读写操作分别用不同的锁,提高并发性。 -...
【SSM实战项目——Java高并发秒杀API详解】 在这个Java实战项目中,我们主要探讨的是如何使用Spring、Spring MVC和MyBatis(SSM)框架来构建一个高并发的秒杀系统。这个项目旨在帮助开发者理解在处理大量并发请求时...
《QD [v20240210] —— HTTP请求定时任务自动执行框架》 QD [v20240210] 是一个基于JavaScript编写的高效且灵活的HTTP请求定时任务自动执行框架。这个框架旨在帮助开发者实现定时执行HTTP请求的任务,以满足各种...
- C: 排它锁 —— 一旦某一资源加上了排它锁,那么只允许该事务读取和修改这一资源,其他事务不能对该资源加任何类型的锁。 - D: 共享锁 —— 允许多个事务同时对同一资源进行读取,但不允许修改。 **正确答案**:...
简单来说,生命周期指的是引用在程序执行过程中的作用域。一旦引用超出了其作用域,它的生命周期就结束了,所引用的值也就变得无效。生命周期的重要性在于: - **内存安全**:通过确保引用始终指向有效的数据,避免...
Java并发框架是Java JDK中内置的一系列用于处理多线程并行执行的工具和类库,自JDK 5.0引入以来,极大地简化了并发编程的复杂性。这个框架由Doug Lea设计,并在JSR-166任务中提出,最终在Tiger(JDK 5)版本中被引入...
在C++中,还可以使用智能指针(如`std::unique_lock`或`std::shared_lock`)与读写锁配合,以实现RAII(Resource Acquisition Is Initialization)风格的代码,这样可以更安全地管理锁的生命周期。 总的来说,...
2. **对象创建与生命周期**:COM组件的创建过程涉及到CoCreateInstance函数,而对象的生命周期管理则涉及引用计数和自动垃圾回收。理解何时创建和释放对象对于避免内存泄漏至关重要。 3. **接口设计**:接口是COM...