public bool CanCarTakeAction(Car car) { lock(this) { if (_ferryAction == Actions.FerryWait) if (Coast == CoastEnum.EastCoast) { if (Cars.Count > 0 && car == Cars.Peek()) { GetCarOut(car); return true; } else return false; } else { if (Cars.Count < FerryCapacity) { GetCarIn(car); return true; } else return false; } else return false; } }
static void Main(string[] args) { Ferry mainFerry = new Ferry(); mainFerry.FerryThread.Start(); Car[] cars = new Car[Ferry.CarsCount]; for (int i = 0; i < Ferry.CarsCount; ++i) { cars[i] = new Car(); cars[i].Index = i+1; cars[i].Ferry = mainFerry; cars[i].CarThread.Start(); } mainFerry.FerryThread.Join(); for (int i = 0; i < Ferry.CarsCount; ++i) { cars[i].CarThread.Join(); } Console.WriteLine("Ended"); Console.ReadLine(); }
private void GetCarOut(Car car) { lock (this) { _ferryAction = Actions.CarMove; Thread.Sleep(500); Cars.Pop(); _ferryAction = Actions.FerryWait; } }