We describe Triveni, a framework and API for integrating threads and events. The design of Triveni is based on an algebra, including preemption combinators, of processes. Triveni is compatible with existing threads standards, such as Pthreads and Java thre
of ce
LightOn
DoorOpenDoorCloseLightOff
SwitchOnSwitchOff
SetTemp(t)
RequestTemp(t)
control
of ceEconomyMode(t)OccupantMode
office:
temperature_stable_officetso;office_controloc;
LOCALSwitchOnSwitchOffLightOnLightOff
RequestTempSetTempDoorOpenDoorClose
INtso||oc
Finally,multipleof cesarecombinedintoanentire oorofanof cebuilding.Theimplementationbelowal-lowsof cestobeaddedonebyone.Theentire ooriscommandedtobeplacedineconomymodeandtobere-storedtooccupantmodeasawhole.However,whileineconomymode,individualof cesmaytemporarilyreverttooccupantmodeduetodooractivity,asdescribedabove.
building oorbuilding oor
of ce
EconomyMode(t)OccupantMode
building_floor:building_floorbf;
officeo;bf||o
Weconcludethisexamplebyrecallingourearliercom-mentsaboutasynchronouscommunication.Inabuildingwithmanyof ces,eachof ceismostlydecoupledfromtheothers.Logically,theonlycommunicationsharedbetween
themisthe
andevents.Furthermore,eachof ceI/Otypicallygenerateseventsasynchronouslywiththeotherof ces.Trivenisupportsthiskindofdecoupling,allowingeachof cetoevolveau-tonomouslyoftheothers.
3TheJavaTriveniimplementation
WehaveimplementedJavaTriveni,arealizationofTriveniinJava.Inthissection,wedescribeinhigh-leveltermsthedesignofJavaTriveni,ignoringcertainimplementa-tiondetailsforthesakeofconceptualclarity.
3.1Theentitiesintheimplementation
Activities.Theclasscapturesthenotionofcommunicatingthreads.Eachmusthavethefollowingcapabilities.(WeexplainbelowJava’sOb-server/Observableprotocolforeventtransmission.)
publicinterfaceControllableextendsRunnable{
voidstart();voidstop();voidsuspend();voidresume();}publicabstractclassCommunicator
extendsObservableimplementsObserver{}
publicabstractclassActivity
extendsCommunicatorimplementsControllable{...}
Notethattherequirementsarenotverysevere,andmanyexistingJavathreadsalreadyqualifyasJavaTriveni
.Forexample,onecanimaginethattheun-derlyingimplementationofanof ceI/Oisan.
EventsandLabels.Asshownabove,com-municatebysendingeventsviatheeventmulticastpor-tionofJava’sObserverprotocol.Observablesemitsandobserversacceptsviaasubscriptionmecha-nism;eachobserversubscribestotheobservableswhose
sitwishestoreceive,andwhenanobservableemitsanitsendsittoitssubscribers.However,theTriveniprogrammerneednotexplicitlyperformthesesubscrip-tions;aswewillexplainlater,Trivenihandlesthesubscrip-tionsautomatically.
Each
comprisesalabelandsomedata.Forin-stance,intheof ceexamplethehaslabel
anddata.Labelsarearbitraryobjects,buttheymusthaveanequalitymethod.
Notethatconcreteeventsourcesandconsumerssuchasgraphicaluser-interfacecomponentsoftenusetheirownevent-handlingmechanism.Fortunately,itispossibletoprovideadapterclassesthatservetoconverttheseintotheformcompatiblewithTriveni.Forexample,Java-Triveniprovidestheadapterclass
(extends)toadaptAWTevents,andonemayaddother
adapterclassesasneeded.
Processes.Aisaspecialcase(i.e.,subclass)
ofthatcanactasanoperandofTrivenicombi-nators.Inotherwords,
esareconstructedinduc-tively(realizedinJavaTriveniviatheCompositepattern).Forinstance,thevariouscomponentsoftheof ceexample
热门文档