diff --git a/src/main/java/com/ioa/IoASystem.java b/src/main/java/com/ioa/IoASystem.java index 940fd7f..e8aa11b 100644 --- a/src/main/java/com/ioa/IoASystem.java +++ b/src/main/java/com/ioa/IoASystem.java @@ -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,34 +196,46 @@ 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 team = teamFormation.formTeam(task); System.out.println("Formed team: " + team); - + if (!team.isEmpty()) { // Create a conversation for the team String conversationId = conversationManager.createConversation(); - + // Add team members to the conversation for (AgentInfo agent : team) { 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) { - 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); + 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(); + } } } diff --git a/target/classes/com/ioa/IoASystem.class b/target/classes/com/ioa/IoASystem.class index df0d0f8..2a4f41c 100644 Binary files a/target/classes/com/ioa/IoASystem.class and b/target/classes/com/ioa/IoASystem.class differ