Skip to content

How to combine many agents onto a single thread

Problem

You've constructed your process out of multiple agents, but want several of them to run on the same thread.

Solution

Combine your agents into a CompositeAgent, and schedule the CompositeAgent in an AgentRunner.

Sample

1
2
3
4
5
6
7
Agent agent1 = new Agent1();
Agent agent2 = new Agent2();
Agent composite = new CompositeAgent(agent1, agent2);
AgentRunner compositeAgentRunner = new AgentRunner(idleStrategy,
        Throwable::printStackTrace,
        errorCounter, composite);
AgentRunner.startOnThread(compositeAgentRunner);

Discussion

The agents within a CompositeAgent share the same thread and idle strategy. Their duty cycles are called sequentially, in the order they are added in the CompositeAgent constructor. When you run a Media Driver with a SHARED Threading Strategy, the Conductor, Sender and Receiver agents are scheduled within a CompositeAgent.

The default thread name is a composite of the roleName provided for each agent. If needed, the CompositeAgent's thread name can be set using roleName().

An alternative approach to do this is to use an AgentInvoker, and run invoke() within the duty cycle of your other agent.

See Also

Back to top