static void Transport() { Random random = new Random(); while (true) { int factoryIndex = random.Next(Factories.Count()); Factory factory = Factories[factoryIndex]; int warehouseIndex = random.Next(Warehouses.Count()); Warehouse warehouse = Warehouses[warehouseIndex]; lock (factory.LockObject) { lock (warehouse.LockObject) { if (factory.NumberOfProducts > 0 && warehouse.CapacityLeft > 0) { warehouse.AddProduct(factory.RemoveProduct()); Console.WriteLine("Product moved from factory " + factoryIndex + " to ware house " + warehouseIndex); } } } } }