Dining Philosopher : Thread DeadLock « Thread « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Thread » Thread DeadLock 
20.13.1.Dining Philosopher
/*
Quote from 

C# and the .NET Framework
by Bob Powell 

*/
using System;
using System.Threading;

public struct PhilosopherData {
  public int        PhilosopherId;
  public Mutex      RightChopStick;
  public Mutex      LeftChopStick;
  public int        AmountToEat;
  public int        TotalFood;
}



public class Philosopher : WorkerThread
{
  public Philosopherobject data : basedata ) { }

  protected override void Run( ) {
    PhilosopherData pd = (PhilosopherData)Data;
    Random r = new Randompd.PhilosopherId );
    Console.WriteLine("Philosopher {0} ready", pd.PhilosopherId );
    WaitHandle[] chopSticks =  new WaitHandle[] { pd.LeftChopStick, pd.RightChopStick };

    whilepd.TotalFood > ) {
      //Get both chop sticks
      WaitHandle.WaitAllchopSticks );
      Console.WriteLine("Philosopher {0} eating {1} of {2} food", pd.PhilosopherId, pd.AmountToEat, pd.TotalFood );
      pd.TotalFood -= pd.AmountToEat;
      Thread.Sleepr.Next(1000,5000) );
      
      //Release the chopsticks
      Console.WriteLine("Philosopher {0} thinking", pd.PhilosopherId);
      pd.RightChopStick.ReleaseMutex( );
      pd.LeftChopStick.ReleaseMutex( );

      //Think for a random time length
      Thread.Sleepr.Next(1000,5000) );
    }
    Console.WriteLine("Philosopher {0} finished", pd.PhilosopherId );
  }
}


public class Restaurant {

  public static void Main( ) {
    Mutex[] chopSticks = new Mutex[5];

    //init the chopSticks
    forint i = 0; i < 5; i++ )
      chopSticks[inew Mutexfalse );
    
    //Create the Five Philosophers
    forint i = 0; i < 5; i++ ) {
      PhilosopherData pd;
      pd.PhilosopherId = i + 1;
      pd.RightChopStick = chopSticksi - >= i - ];
      pd.LeftChopStick = chopSticks[i];
      pd.AmountToEat = 5;
      pd.TotalFood = 35;
      Philosopher p = new Philosopherpd );
      p.Start( );
    }

    Console.ReadLine( );
  }
}
public abstract class WorkerThread {

  private object  ThreadData;
  private Thread  thisThread;


  //Properties
  public object Data {
    get return ThreadData; }
    set ThreadData = value; }
  }

  public object IsAlive {
    get return thisThread == null false : thisThread.IsAlive; }
  }

  /// <summary>
  /// Constructors
  /// </summary>

  public WorkerThreadobject data ) {
    this.ThreadData = data;
  }

  public WorkerThread( ) {
    ThreadData = null;
  }

  /// <summary>
  /// Public Methods
  /// </summary>
  
  /// <summary>
  /// Start the worker thread
  /// </summary>
  public void Start( ) {
    thisThread = new Threadnew ThreadStartthis.Run ) );
    thisThread.Start();
  }

  /// <summary>
  /// Stop the current thread.  Abort causes
  /// a ThreadAbortException to be raised within
  /// the thread
  /// </summary>
  public void Stop( ) {
    thisThread.Abort( );
    whilethisThread.IsAlive ;
    thisThread = null;
  }

  /// <summary>
  /// To be implemented by derived threads
  /// </summary>
  protected abstract void Run( );
}
Philosopher 1 ready
Philosopher 1 eating 5 of 35 food
Philosopher 2 ready
Philosopher 3 ready
Philosopher 3 eating 5 of 35 food
Philosopher 4 ready
Philosopher 5 ready
Philosopher 1 thinking
Philosopher 5 eating 5 of 35 food
Philosopher 3 thinking
Philosopher 2 eating 5 of 35 food
Philosopher 5 thinking
Philosopher 4 eating 5 of 35 food
^CTerminate batch job (Y/N)? n
20.13.Thread DeadLock
20.13.1.Dining Philosopher
20.13.2.A sure-fire deadlock
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.