Java actor model and simple example of message passing

Source: Internet
Author: User

[Java]
Import java. util. HashMap;
Import java. util. Map;
 
Import com. ibm. actor. extends actactor;
Import com. ibm. actor. Actor;
Import com. ibm. actor. DefaultActorManager;
Import com. ibm. actor. DefaultMessage;
Import com. ibm. actor. Message;
 
Public class TestActor extends actactor {
 
Private Actor;
 
Public void setTestActor (Actor sendTo ){
This. to = sendTo;
}
 
@ Override
Public void activate (){
Super. activate ();
}
 
@ Override
Public void deactivate (){
Logger. trace ("TestActor deactivate: % s", getName ());
Super. deactivate ();
}
 
@ Override
Protected void runBody (){
DefaultMessage m = new DefaultMessage ("message-> from:" + getName (), 20 );
GetManager (). send (m, null, this );
}
 
@ Override
Protected void loopBody (Message m ){
String subject = m. getSubject ();
Int count = (Integer) m. getData ();
If (count> 0 ){
M = new DefaultMessage ("message-> from:" + getName (), count-1 );
System. out. println (getName () + ":" + subject + "data:" + m. getData ());
GetManager (). send (m, this, );
}
 
}
 
Public static void main (String [] args ){
DefaultActorManager am = new DefaultActorManager ();
Map <String, Actor> actors = new HashMap <String, Actor> ();
Try {
Map <String, Object> options = new HashMap <String, Object> ();
Options. put (DefaultActorManager. ACTOR_THREAD_COUNT, 2 );
Am. initialize (options );
TestActor a = (TestActor) am. createActor (TestActor. class, "actor0 ");
Actors. put (a. getName (), );
TestActor B = (TestActor) am. createActor (TestActor. class, "actor1 ");
Actors. put (B. getName (), B );
A. setTestActor (B );
B. setTestActor ();
For (String key: actors. keySet ()){
Am. startActor (actors. get (key ));
}
Thread. sleep (10000000 );
// Am. terminateAndWait ();
} Catch (Exception e ){
}
} Www.2cto.com
}

Output:
[Html]
Actor0: message-> from: actor0 data: 19
Actor1: message-> from: actor1 data: 19
Actor1: message-> from: actor0 data: 18
Actor0: message-> from: actor1 data: 18
Actor1: message-> from: actor0 data: 17
Actor0: message-> from: actor1 data: 17
Actor0: message-> from: actor1 data: 16
Actor1: message-> from: actor0 data: 16
Actor1: message-> from: actor0 data: 15
Actor0: message-> from: actor1 data: 15
Actor0: message-> from: actor1 data: 14
Actor1: message-> from: actor0 data: 14
Actor0: message-> from: actor1 data: 13
Actor1: message-> from: actor0 data: 13
Actor0: message-> from: actor1 data: 12
Actor1: message-> from: actor0 data: 12
Actor1: message-> from: actor0 data: 11
Actor0: message-> from: actor1 data: 11
Actor0: message-> from: actor1 data: 10
Actor1: message-> from: actor0 data: 10
Actor0: message-> from: actor1 data: 9
Actor1: message-> from: actor0 data: 9
Actor1: message-> from: actor0 data: 8
Actor0: message-> from: actor1 data: 8
Actor0: message-> from: actor1 data: 7
Actor1: message-> from: actor0 data: 7
Actor1: message-> from: actor0 data: 6
Actor0: message-> from: actor1 data: 6
Actor0: message-> from: actor1 data: 5
Actor1: message-> from: actor0 data: 5
Actor1: message-> from: actor0 data: 4
Actor0: message-> from: actor1 data: 4
Actor1: message-> from: actor0 data: 3
Actor0: message-> from: actor1 data: 3
Actor1: message-> from: actor0 data: 2
Actor0: message-> from: actor1 data: 2
Actor1: message-> from: actor0 data: 1
Actor0: message-> from: actor1 data: 1
Actor1: message-> from: actor0 data: 0
Actor0: message-> from: actor1 data: 0

 


Author: zhongweijian

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.