// Returnerar true om spelaren skall ta upp korten på skräphögen (card), annars false för att dra kort från leken. public override bool TaUppKort(Card card) { Card discardCard = EMethods.DiscardCardAdditional(Hand, card); if (discardCard == card) { return(false); } return(true); }
public void Run(EMethods method) { watch = Stopwatch.StartNew(); Log("Starting..."); const int dueTime = 1000; const int checkPeriod = 2000; threadingTimer = new Timer(Observe, null, dueTime, checkPeriod); var philosophers = new Task[PhilosophersAmount]; var cancelTokenSource = new CancellationTokenSource(); var autoResetEvent = new AutoResetEventSolution(); var monitorSolution = new MonitorSolution(); var semaphorSlimSolution = new SemaphoreSlimSolution(); var runActions = new Func <int, Task>[] { (i) => Task.Run(() => RunDeadlock(i, cancelTokenSource.Token)), (i) => Task.Run(() => RunStarvation(i, cancelTokenSource.Token)), (i) => Task.Run(() => RunSpinLock(i, cancelTokenSource.Token)), (i) => Task.Run(() => RunInterlocked(i, cancelTokenSource.Token)), (i) => Task.Run(() => autoResetEvent.Run(i, cancelTokenSource.Token)), (i) => Task.Run(() => monitorSolution.Run(i, cancelTokenSource.Token)), (i) => Task.Run(() => semaphorSlimSolution.Run(i, cancelTokenSource.Token)), }; Log($"Method {method}"); for (int i = 0; i < PhilosophersAmount; i++) { int icopy = i; philosophers[i] = runActions[(int)method](icopy); } Console.WriteLine("Press any key to exit..."); Console.ReadLine(); cancelTokenSource.Cancel(); try { const int timeToWaitMS = 3000; Task.WaitAll(philosophers, timeToWaitMS); watch.Stop(); } catch (Exception e) { Log("Exception: " + e.Message); } Log($"Food {string.Join(' ', eatenFood)}, thoughts {string.Join(' ', thoughts)}"); Console.WriteLine($"Elapsed time: {watch.ElapsedMilliseconds}"); Console.WriteLine($"Wait time: {string.Join(' ', _waitTime)}"); if (_waitTime.Any(i => i == 0)) { Console.WriteLine("Failed to run some philosopher(s)"); } else { Console.WriteLine( $"Total wait time: {_waitTime.Sum()}," + $" total wait / elapsed: {(double)_waitTime.Sum() / watch.ElapsedMilliseconds}"); } }
// Returnerar det kort som skall kastas av de fyra som finns på handen public override Card KastaKort() { Card discardCard = EMethods.DiscardCard(Hand); return(discardCard); }