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

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. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / CSharp Tutorial » Thread » Thread DeadLock 
20. 14. 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. 14. Thread DeadLock
20. 14. 1. Dining Philosopher
20. 14. 2. A sure-fire deadlock
w___ww___.java_2_s.___c___om___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.