Threading the team based system
This commit is contained in:
parent
a68d6fd419
commit
2859a502ba
|
@ -38,6 +38,11 @@ import org.springframework.messaging.simp.SimpMessagingTemplate;
|
|||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
@SpringBootApplication
|
||||
public class IoASystem {
|
||||
|
||||
|
@ -191,6 +196,9 @@ public class IoASystem {
|
|||
|
||||
);
|
||||
|
||||
// Create a thread pool
|
||||
ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||
|
||||
for (Task task : tasks) {
|
||||
System.out.println("\nProcessing task: " + task.getDescription());
|
||||
List<AgentInfo> team = teamFormation.formTeam(task);
|
||||
|
@ -205,20 +213,29 @@ public class IoASystem {
|
|||
conversationManager.addParticipant(conversationId, agent);
|
||||
}
|
||||
|
||||
// Assign the task to all team members
|
||||
// Create a CountDownLatch to wait for all agents to complete their tasks
|
||||
CountDownLatch latch = new CountDownLatch(team.size());
|
||||
|
||||
// Assign the task to all team members and execute in parallel
|
||||
for (AgentInfo agent : team) {
|
||||
executorService.submit(() -> {
|
||||
try {
|
||||
Task agentTask = new Task(task.getId() + "_" + agent.getId(), task.getDescription(), task.getRequiredCapabilities(), task.getRequiredTools());
|
||||
agentTask.setAssignedAgent(agent);
|
||||
taskManager.addTask(agentTask);
|
||||
taskManager.executeTask(agentTask.getId(), conversationId);
|
||||
} finally {
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Start the conversation
|
||||
conversationManager.startConversation(conversationId, "Let's work on the task: " + task.getDescription());
|
||||
|
||||
// Wait for the conversation to finish (you might want to implement a more sophisticated mechanism)
|
||||
// Wait for all agents to complete their tasks
|
||||
try {
|
||||
Thread.sleep(30000); // Wait for 30 seconds
|
||||
latch.await(5, TimeUnit.MINUTES); // Wait for up to 5 minutes
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -230,5 +247,15 @@ public class IoASystem {
|
|||
System.out.println("No suitable agents found for this task. Consider updating the agent pool or revising the task requirements.");
|
||||
}
|
||||
}
|
||||
|
||||
// Shutdown the executor service
|
||||
executorService.shutdown();
|
||||
try {
|
||||
if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
|
||||
executorService.shutdownNow();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
executorService.shutdownNow();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user