[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