public void Eat() { if (TakeWidelecInPrawyHand()) { if (TakeWidelecInLewyHand()) { this.State = PhilosopherState.Eating; Console.WriteLine("{0} je z {1} i {2}", Name, PrawyWidelec.WidelecID, LewyWidelec.WidelecID); Thread.Sleep(rand.Next(5000, 10000)); contCount = 0; PrawyWidelec.Put(); LewyWidelec.Put(); } else { Thread.Sleep(rand.Next(100, 400)); if (TakeWidelecInLewyHand()) { this.State = PhilosopherState.Eating; Console.WriteLine("{0} je z {1} i {2}", Name, PrawyWidelec.WidelecID, LewyWidelec.WidelecID); Thread.Sleep(rand.Next(5000, 10000)); contCount = 0; PrawyWidelec.Put(); LewyWidelec.Put(); } else { PrawyWidelec.Put(); } } } else { if (TakeWidelecInLewyHand()) { Thread.Sleep(rand.Next(100, 400)); if (TakeWidelecInPrawyHand()) { this.State = PhilosopherState.Eating; Console.WriteLine("{0} je z {1} i {2}", Name, PrawyWidelec.WidelecID, LewyWidelec.WidelecID); Thread.Sleep(rand.Next(5000, 10000)); contCount = 0; PrawyWidelec.Put(); LewyWidelec.Put(); } else { LewyWidelec.Put(); } } } Think(); }
public Philosopher(Widelec PrawyWidelec, Widelec LewyWidelec, string name, int starvThreshold) { this.PrawyWidelec = PrawyWidelec; this.LewyWidelec = LewyWidelec; Name = name; State = PhilosopherState.Thinking; StarvationThreshold = starvThreshold; }
// Constructor public Philosopher(string name, int starvationTreshold, Chopstick Left, Chopstick Right) { this.name = name; this.starvationTreshold = starvationTreshold; LeftChopstick = Left; RightChopstick = Right; state = PhilosopherState.Thinking; }
public void Think() { this.State = PhilosopherState.Thinking; Console.WriteLine("{0} mysli o {1}", Name, Thread.CurrentThread.Priority.ToString()); Thread.Sleep(rand.Next(2500, 20000)); contCount++; if (contCount > StarvationThreshold) { Console.WriteLine("{0} gloduje", Name); } Eat(); }
public void canEat() { // Philosopher is holding chopsticks in both hands so he can start eating this.state = PhilosopherState.Eating; Console.WriteLine("{0} je uzywajac w lewej rece {1} a w prawej {2}", name, LeftChopstick.name, RightChopstick.name); // Eating takes some time Thread.Sleep(new Random().Next(3000, 10000)); // Reset thinking counter thinkingCombo = 0; // Place back the chopsticks RightChopstick.Put(); LeftChopstick.Put(); }
public void Think() { // Start thinking (it takes some time) this.state = PhilosopherState.Thinking; Console.WriteLine("{0} mysli", name); Thread.Sleep(new Random().Next(6000, 20000)); thinkingCombo++; if (thinkingCombo > starvationTreshold) { Console.WriteLine("{0} zaczyna glodowac :-(", name); } Eat(); }
/// <summary> /// Update philosoher state and corresponding UI elements /// </summary> public void SetState(PhilosopherState state) { State = state; switch (State) { case PhilosopherState.Hungry: statusLabel.Dispatcher.Invoke(new Action(() => statusLabel.Content = "Hungry")); foodText.Dispatcher.Invoke(new Action(() => foodText.Text = FoodCount.ToString())); break; case PhilosopherState.Eating: rSprite.Dispatcher.Invoke(new Action(() => rSprite.Visibility = Visibility.Visible)); lSprite.Dispatcher.Invoke(new Action(() => lSprite.Visibility = Visibility.Visible)); pSprite.Dispatcher.Invoke(new Action(() => pSprite.Visibility = Visibility.Visible)); statusLabel.Dispatcher.Invoke(new Action(() => statusLabel.Content = "Eating")); foodText.Dispatcher.Invoke(new Action(() => foodText.Text = FoodCount.ToString())); FoodCount--; Thread.Sleep(EatMs); break; case PhilosopherState.Thinking: rSprite.Dispatcher.Invoke(new Action(() => rSprite.Visibility = Visibility.Hidden)); lSprite.Dispatcher.Invoke(new Action(() => lSprite.Visibility = Visibility.Hidden)); pSprite.Dispatcher.Invoke(new Action(() => pSprite.Visibility = Visibility.Hidden)); statusLabel.Dispatcher.Invoke(new Action(() => statusLabel.Content = "Thinking...")); foodText.Dispatcher.Invoke(new Action(() => foodText.Text = FoodCount.ToString())); Thread.Sleep(ThinkMs); break; case PhilosopherState.Done: statusLabel.Dispatcher.Invoke(new Action(() => statusLabel.Content = "Done.")); foodText.Dispatcher.Invoke(new Action(() => foodText.Text = FoodCount.ToString())); break; default: break; } }
public PhilosopherEventArgs(ForksState forksState, PhilosopherState state, int number) { ForksState = forksState; Number = number; State = state; }