1.actor并发模型的应用场景?
2.actor的原理?思维方式改变?
3.actor最简单的demo?
4.如何应用好actor模型,要注意哪些地方
1.actor并发模型的应用场景?
2.actor的原理?思维方式改变?
3.actor最简单的demo?
4.如何应用好actor模型,要注意哪些地方
适合有状态或者称可变状态的业务场景,如果用DDD术语,适合聚合根,具体案例如订单,订单有状态,比如未付款未发货,已经付款未发货,已付款已发货,导致订单状态的变化是事件行为,比如付款行为导致顶大状态切换到"已经付款未发货"。
如果知晓GOF设计模式的状态模式,就更好理解有态概念。
2.actor的原理?思维方式改变?
行为导致状态变化,行为执行是依靠线程,比如用户发出一个付款的请求,服务器后端派出一个线程来执行付款请求,携带付款的金额和银行卡等等信息,当付款请求被成功完成后,线程还要做的事情就是改变订单状态,这时线程访问订单的一个方法比如changeState。
如果后台有管理员同时修改这个订单状态,那么实际有两个线程共同访问同一个数据,这时就必须锁,比如我们在changeState方法前加上sychronized这样同步语法。
使用同步语法坏处是每次只能一个线程进行处理,如同上厕所,只有一个蹲坑,人多就必须排队,这种情况性能很低。
如何避免锁?
避免changeState方法被外部两个线程同时占用访问,那么我们自己设计专门的线程守护订单状态,而不是普通方法代码,普通方法代码比较弱势,容易被外部线程hold住,而我们设计的这个对象没有普通方法,只有线程,这样就变成Order的守护线程和外部访问请求线程的通讯问题了。
Actor采取的这种类似消息机制的方式,实际在守护线程和外部线程之间有一个队列,俗称信箱,外部线程只要把请求放入,守护线程就读取进行处理。
这种异步高效方式是Actor基本原理,以ERlang和Scala语言为主要特征,他们封装得更好,类似将消息队列微观化了。
我个人认为要使用好Actor,还是要树立自己对有态和无态的敏感性,这是几年前我宣传EJB的有态和无态Bean强调的一点,如果没有这个敏感性,按照DDD第一找出聚合根的分析方法也能抓住重点。
当然这些思维的前提是抛弃数据库中心思维,不要老是想着把状态存在数据库中,然后用SQL不断修改,这是很低效的,也是有锁,比Java等语言的同步锁性能更差。
以我个人来说经历的步骤如下:
1.用数据表一个字段来表示状态,比如1表示已付款未发货,2表示已付款已发货,然后用户来一个请求用SQL修改。
2.用ORM实现,比如Hibernate JPA来修改状态,虽然不用SQL了,但是Hibernate的悲观锁和乐观锁也让人抓狂。
3.彻底抛弃数据库,直接在内存缓存中进行修改,使用Java的同步锁,性能还是不够,吞吐量上不去。
4.Actor模型。
[该贴被banq于2013-06-25 13:50修改过]
这。。。如果这两个线程在一个jvm里还行,可是遇到分布式的情况咋办啊。。?尤其是spring提倡的那种分布式
这是可伸缩的,Actor对象(聚合根)本身有类似IO进出,就像信箱有收件箱和发件箱一样,Actor可以通过发件箱向一个MQ等消息总线发送消息就可实现分布式。
发件箱的线程队列实现可借助OneToOneConcurrentArrayQueue,只有一个订阅者和发布者,发布者是Actor自己,订阅者是一个Socket端口或MQ消息总线的发布者。http://www.jdon.com/45504
分布式网络是否可靠?http://www.jdon.com/45519
区分不变与可变,也就是区分无态和有态,也就是区分实体(可变)与值对象(不变),是统领一切的编程法门(无论是多线程还是分布式)。
[该贴被banq于2013-06-28 05:32修改过]
类似Actor/CSP的消息传递机制。Go语言中也提供了这样的功能。Go的并发实体叫做goroutine,类似coroutine,但不需要自己调度。Runtime自己就会把goroutine调度到系统的线程上去运行,多个goroutine共享一个线程。如果有一个要阻塞,系统就会自动把其他的goroutine调度到其他的线程上去。
一些名词定义:
1. Processes, threads, green threads, protothreads, fibers, coroutines: what's the difference?
• Process: OS-managed (possibly) truly concurrent, at least in the presence of suitable hardware support. Exist within their own address space.
• Thread: OS-managed, within the same address space as the parent and all its other threads. Possibly truly concurrent, and multi-tasking is pre-emptive.
• Green Thread: These are user-space projections of the same concept as threads, but are not OS-managed. Probably not truly concurrent, except in the sense that there may be multiple worker threads or processes giving them CPU time concurrently, so probably best to consider this as interleaved or multiplexed.
• Protothreads: I couldn't really tease a definition out of these. I think they are interleaved and program-managed, but don't take my word for it. My sense was that they are essentially an application-specific implementation of the same kind of "green threads" model, with appropriate modification for the application domain.
• Fibers: OS-managed. Exactly threads, except co-operatively multitasking, and hence not truly concurrent.
• Coroutines: Exactly fibers, except not OS-managed.
Coroutines are computer program components that generalize subroutines to allow multiple entry points for suspending and resuming execution at certain locations. Coroutines are well-suited for implementing more familiar program components such as cooperative tasks, iterators, infinite lists and pipes.
• Continuation: An abstract representation of the control state of a computer program.
A continuation reifies the program control state, i.e. the continuationis a data structure that represents the computational process at a given point in the process' execution; the created data structure can be accessed by the programming language, instead of being hidden in the runtime environment. Continuations are useful for encoding other control mechanisms in programming languages such as exceptions, generators, coroutines, and so on.
The "current continuation" or "continuation of the computation step" is the continuation that, from the perspective of running code, would be derived from the current point in a program's execution. The term continuations can also be used to refer to first-class continuations, which are constructs that give a programming language the ability to save the execution state at any pointand return to that point at a later point in the program.(yield keywork in some languages, such as c# or python)
•Goroutines: They claim to be unlike anything else, but they seem to be exactly green threads, as in, process-managed in a single address space and multiplexed onto system threads. Perhaps somebody with more knowledge of Go can cut through the marketing material.
我不能认同这种观点,事件是一个语义词,用事件模式编程,能够更符合日常生活中事件用语,比如XXX发生事件了,事件的发生是随机的,那么响应处理也是随机的,事件一词的背后本质就是异步。
事件编程关键是能够用异步思维处理分析业务,这就能高效率利用硬件,虽然事件落实到硬件或Linux底层都是一样。这根本是两个层次的问题。
打个比喻,快克是感冒药,其成分是乙酰氨基酚,但是“快克”肯定比“乙酰氨基酚”更易于被人接受,易于被人使用。而事件相对线程一词,类似快克相对乙酰氨基酚一样。
从以人为中心的软件角度来看,事件巧妙地打通了业务需求和软件实现之间的鸿沟,既准确表达需求中概念,又能实现高性能并发,充分利用多CPU。
如果从以CPU机器为中心的软件角度看,事件和线程没有什么区别,用脚趾头都能想通,不知为何还有人要去证明什么?无非是想把程序员绕进以CPU机器为核心的僵化思维体系而已。
呵呵,我以为你是转发的,我们角度不同,不过你的角度更经典classic,有学院风格,我的好像是有点"野路子",但是个人感觉更实用些,不过初学者可能不太容易理解。