I have a loop, but it is too heavy so I would like to share the same in multiple threads, but I have no idea how to do it.
while(!labyrinth.exit(bob3) && !labyrinth.exit(bob2)) {
Collection<Room> accessibleRooms = labyrinth.accessibleRooms(bob3);
if (bob3.canMove()) {
destination = bob3.makeChoice(accessibleRooms);
}
if (destination != bob3.getPosition()) {
destination.receive(bob3);
}
accessibleRooms = labyrinth.accessibleRooms(bob2);
if (bob2.canMove()) {
destination = bob2.makeChoice(accessibleRooms);
}
if (destination != bob2.getPosition()) {
destination.receive(bob2);
}
}
As you can see, in this loop we have two operations that are identical so is it possible to make each one use a different thread.
1 Answer