BlockingQueueImpl.cs :  » Network-Clients » GROF » grof » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » GROF 
GROF » grof » BlockingQueueImpl.cs
using System;
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;

namespace grof{
  namespace util 
  {

    /// <summary>
    /// This class represents an implementation of the
        /// <c>IBlockingQueue</c> interface.
    /// </summary>
    public class BlockingQueueImpl<T> : IBlockingQueue<T>
    {
      /// <summary>
      /// The protocol name.
      /// </summary>
      private string name;
      
            /// <summary>
            /// The internal data structure for queuing
            /// elements.
            /// </summary>
      private Queue<T> queue;

            /// <summary>
            /// A semaphore for synchronizing
            /// the access to the queue.
            /// </summary>
      private object semaphore;

            /// <summary>
            /// Indicates whether the queue was stopped.
            /// </summary>
      private bool stopped;
      
      /// <summary>
      /// Creates instances of class
            /// <c>BlockingQueueImpl</c>.
      /// </summary>
      public BlockingQueueImpl( string name )
      {
        this.name = name;
        this.semaphore = new object();
        this.queue = new Queue<T>();
        this.stopped = false;
      }

            /// <summary>
            /// Puts an element into the queue. If the queue
            /// was stopped no further elements can be 
            /// queued, because a <c>BlockingQueueStoppedException</c>
            /// is thrown.
            /// </summary>
            /// <param name="element">The element which is cached
            /// in the queue.</param>
      public void Put( T element )
      {      
        Debug.WriteLine( "[BlockingQueueImpl#Put] [" + this.name + "] Called." );  
        try 
        {
          Monitor.Enter( this.semaphore );
          if ( this.stopped == false )
          {
            this.queue.Enqueue( element );
            Monitor.PulseAll( this.semaphore );
            
          } else
          {
            throw new BlockingQueueStoppedException( "Queue was stopped." );
          }
        } finally {
          Monitor.Exit( this.semaphore );
        }
      }

            /// <summary>
            /// Retrieves an element from the queue. If the
            /// queue was stopped, no further elements can be
            /// retrieved, because a <c>BlockingQueueStoppedException</c>
            /// is thrown.
            /// </summary>
            /// <returns>The retrieved element.</returns>
      public T Take() 
      {
        Debug.WriteLine( "[BlockingQueueImpl#Take] [" + this.name + "] Called." );  
        Monitor.Enter( this.semaphore );
        try {
          
          while ( this.queue.Count == 0 )
          {            
            if ( this.stopped )
            {
              throw new BlockingQueueStoppedException( "Queue was stopped" );
            }
            Monitor.Wait( this.semaphore );
            
          }
          if ( this.stopped )
          {
            throw new BlockingQueueStoppedException( "Queue was stopped" );
          }
          T element = this.queue.Dequeue();
          return element;
        } finally{
          Monitor.Exit( this.semaphore );
        }              
      }

            /// <summary>
            /// Returns the size of the queue.
            /// </summary>
            /// <returns>The number of cached elements.</returns>
      public int Size()
      {
        Debug.WriteLine( "[BlockingQueueImpl#Size] [" + this.name + "] Called." );  
        Monitor.Enter( this.semaphore );        
        try 
        {
          return this.queue.Count;
        } finally 
        {
          Monitor.Exit( this.semaphore );
        }
      }

            /// <summary>
            /// Stops the queue, that means no further elements
            /// can be queued or dequeued in and from queue.
            /// </summary>
      public void Stop()
      {
        Debug.WriteLine( "[BlockingQueueImpl#Stop] [" + this.name + "] Called." );      
        Monitor.Enter( this.semaphore );
        this.stopped = true;
        Monitor.PulseAll( this.semaphore );
        Monitor.Exit( this.semaphore );
      }
      
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.