Пример #1
0
 private static void Writer(BoundedBuffer buffer)
 {
     for (int i = 0; i < 20; i++)
     {
         buffer.Put("hello " + i);
     }
 }
Пример #2
0
 private static void Reader(BoundedBuffer buffer)
 {
     for (int i = 0; i < 10; i++)
     {
         buffer.Take();
     }
 }
Пример #3
0
        /// <summary>
        ///     Based on messageset per topic/partition, group it by topic and send out.
        ///     Sync with java/scala version , do group by topic inside this class.
        ///     https://git-wip-us.apache.org/repos/asf?p=kafka.git;a=blob;f=core/src/main/scala/kafka/api/ProducerRequest.scala;h=570b2da1d865086f9830aa919a49063abbbe574d;hb=HEAD
        ///     private lazy val dataGroupedByTopic = data.groupBy(_._1.topic)
        /// </summary>
        /// <param name="correlationId"></param>
        /// <param name="clientId"></param>
        /// <param name="requiredAcks"></param>
        /// <param name="ackTimeout"></param>
        /// <param name="messagesPerTopic"></param>
        public ProducerRequest(int correlationId,
                               string clientId,
                               short requiredAcks,
                               int ackTimeout,
                               IDictionary <TopicAndPartition, BufferedMessageSet> messagesPerTopic)
        {
            var topicName   = string.Empty;
            var partitionId = -1;
            var topics      = new Dictionary <string, List <PartitionData> >();

            foreach (var keyValuePair in messagesPerTopic)
            {
                topicName   = keyValuePair.Key.Topic;
                partitionId = keyValuePair.Key.PartitionId;
                var messagesSet = keyValuePair.Value;
                if (!topics.ContainsKey(topicName))
                {
                    topics.Add(topicName, new List <PartitionData>()); //create a new list for this topic
                }
                topics[topicName].Add(new PartitionData(partitionId, messagesSet));
            }

            VersionId     = CurrentVersion;
            CorrelationId = correlationId;
            ClientId      = clientId;
            RequiredAcks  = requiredAcks;
            AckTimeout    = ackTimeout;
            Data          = topics.Select(kv => new TopicData(kv.Key, kv.Value));
            var length = GetRequestLength();

            RequestBuffer = new BoundedBuffer(length);
            WriteTo(RequestBuffer);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="EncryptedNetworkSession"/> class with no specified socket.
        /// </summary>
        /// <remarks>
        /// Call <see cref="AttachSocket(Socket)"/> before starting the network operations.
        /// </remarks>
        protected EncryptedNetworkSession()
        {
            _packetBuffer = new BoundedBuffer();
            _headerBuffer = new BoundedBuffer(4);

            _baseSession = CreateInnerSession();

            _isDisposed = false;
        }
Пример #5
0
        public void TestMethod1()
        {
            BoundedBuffer buf = new BoundedBuffer(4);

            Producer prod = new Producer(buf, 10);
            Consumer con  = new Consumer(buf, 10);

            Parallel.Invoke(prod.Run, con.Run);
        }
Пример #6
0
        public void TestMethod1()
        {
            BoundedBuffer buf = new BoundedBuffer(4);

            Producer prod = new Producer(buf, 10);
            Consumer con = new Consumer(buf);

            Parallel.Invoke(prod.Run, con.Run);
        }
Пример #7
0
        static void Main(string[] args)
        {
            BoundedBuffer buf = new BoundedBuffer(4);

            Producer prod = new Producer(buf, 100);

            Consumer con = new Consumer(buf);

            Parallel.Invoke(prod.run(), con.run());
        }
 /// <summary>
 /// Инициализирует новый экземпляр класса CompressionFilesWithConveyor
 /// </summary>
 /// <param name="sourceFilePath">Имя исходного файла</param>
 /// <param name="destinationFilePath">Имя конечного файла</param>
 public CompressionFilesWithConveyor(string sourceFilePath, string destinationFilePath)
 {
     _sourceFilePath = sourceFilePath;
     _destinatinFilePath = destinationFilePath;
     _readingBufferCompressing = new BoundedBuffer<byte[]>(4);
     _writingBuffer = new BoundedBuffer<byte[]>(4);
     _readingBufferDecompressing = new BoundedBuffer<CompressingData>(4);
     _gzipThread = new Thread[2];
     _length = 0;
     _parts = 0;
     _success = 1;
     _out = "Ошибка компрессии/декомпрессии";
 }
Пример #9
0
 /// <summary>
 ///     Initializes a new instance of the OffsetRequest class.
 /// </summary>
 public OffsetRequest(Dictionary <string,
                                  List <PartitionOffsetRequestInfo> > requestInfo,
                      short versionId   = 0,
                      int correlationId = 0,
                      string clientId   = "",
                      int replicaId     = -1)
 {
     VersionId     = versionId;
     ClientId      = clientId;
     CorrelationId = correlationId;
     ReplicaId     = replicaId;
     RequestInfo   = requestInfo;
     RequestBuffer = new BoundedBuffer(GetRequestLength());
     WriteTo(RequestBuffer);
 }
Пример #10
0
    static void Main(string[] args)
    {
        BoundedBuffer boundedBuffer = new BoundedBuffer();

        MyProducer prod           = new MyProducer(boundedBuffer, 20);
        Thread     producerThread = prod.CreateProducerThread();

        MyConsumer cons           = new MyConsumer(boundedBuffer, 20);
        Thread     consumerThread = cons.CreateConsumerThread();

        producerThread.Start();
        consumerThread.Start();

        Console.ReadLine();
    }
Пример #11
0
        public void TestChallenge14WithFix()
        {
            this.Test(() =>
            {
                BoundedBuffer buffer = new BoundedBuffer(true);
                var tasks            = new List <Task>
                {
                    Task.Run(() => Reader(buffer)),
                    Task.Run(() => Reader(buffer)),
                    Task.Run(() => Writer(buffer))
                };

                Task.WaitAll(tasks.ToArray());
            },
                      configuration: GetConfiguration().WithTestingIterations(100));
        }
Пример #12
0
        public BreweryWorker()
        {
            rnd = new Random(DateTime.Now.Millisecond);

            unwashedBottles = new BoundedBuffer <Bottle>(BUFFER_LENGTH);
            washedBottles   = new BoundedBuffer <Bottle>(BUFFER_LENGTH);
            filledBottles   = new BoundedBuffer <Bottle> [NUMBER_OF_FILLING_TOPPING_THREADS];
            toppedIsReady   = new Semaphore[NUMBER_OF_FILLING_TOPPING_THREADS];
            for (int i = 0; i < NUMBER_OF_FILLING_TOPPING_THREADS; i++)
            {
                filledBottles[i] = new BoundedBuffer <Bottle>(1);
                toppedIsReady[i] = new Semaphore(1, 1);
            }
            toppedBottles = new BoundedBuffer <Bottle>(BUFFER_LENGTH);
            boxOfBottles  = new BoundedBuffer <List <Bottle> >(BUFFER_LENGTH / 24);
        }
Пример #13
0
        private static List<Thread> CreateThreads(List<long> results, BoundedBuffer<long> numbersToCheck)
        {
            var threadCount = Environment.ProcessorCount*2;

            Console.WriteLine("Using {0} compute threads and 1 I/O thread", threadCount);

            var threads =
                (from threadNumber in Sequence.Create(0, threadCount)
                    let calculator = new IsNumberPrimeCalculator(results, numbersToCheck,resultMutex)
                    let newThread =
                        new Thread(calculator.CheckIfNumbersArePrime) {
                            IsBackground = true,
                            Priority = ThreadPriority.BelowNormal
                        }
                    select newThread).ToList();
            return threads;
        }
Пример #14
0
        public ProducerRequest(short versionId,
                               int correlationId,
                               string clientId,
                               short requiredAcks,
                               int ackTimeout,
                               IEnumerable <TopicData> data)
        {
            VersionId     = versionId;
            CorrelationId = correlationId;
            ClientId      = clientId;
            RequiredAcks  = requiredAcks;
            AckTimeout    = ackTimeout;
            Data          = data;
            var length = GetRequestLength();

            RequestBuffer = new BoundedBuffer(length);
            WriteTo(RequestBuffer);
        }
Пример #15
0
        public FetchRequest(int correlationId,
                            string clientId,
                            int maxWait,
                            int minBytes,
                            Dictionary <string, List <PartitionFetchInfo> > fetchInfos)
        {
            VersionId     = CurrentVersion;
            CorrelationId = correlationId;
            ClientId      = clientId;
            ReplicaId     = -1;
            MaxWait       = maxWait;
            MinBytes      = minBytes;
            OffsetInfo    = fetchInfos;
            var length = GetRequestLength();

            RequestBuffer = new BoundedBuffer(length);
            WriteTo(RequestBuffer);
        }
Пример #16
0
        public void Run(NumberReader reader)
        {
            var results = new List<long>();
            var numbersToCheck = new BoundedBuffer<long>(bufferSize);

            StartComputationThreads(results, numbersToCheck);

            var progressMonitor = new ProgressMonitor(results,resultMutex);

            new Thread(progressMonitor.Run) {IsBackground = true}.Start();

            foreach (var value in reader.ReadIntegers()) {
                numbersToCheck.Produce (value);
            }

            while (numbersToCheck.Count() > 0) {
                Thread.Sleep(100); // wait for the computation to complete.
            }
            Console.WriteLine("{0} of the numbers were prime", progressMonitor.TotalCount);
        }
Пример #17
0
        private TopicMetadataRequest(IEnumerable <string> topics, short versionId, int correlationId, string clientId)
        {
            if (topics == null)
            {
                throw new ArgumentNullException("topics", "List of topics cannot be null.");
            }

            if (!topics.Any())
            {
                throw new ArgumentException("List of topics cannot be empty.");
            }

            Topics             = new List <string>(topics);
            this.versionId     = versionId;
            this.correlationId = correlationId;
            this.clientId      = clientId;
            var length = GetRequestLength();

            RequestBuffer = new BoundedBuffer(length);
            WriteTo(RequestBuffer);
        }
Пример #18
0
        public void TestChallenge14WithDeadlock()
        {
            this.TestWithError(() =>
            {
                BoundedBuffer buffer = new BoundedBuffer(false);
                var tasks            = new List <Task>
                {
                    Task.Run(() => Reader(buffer)),
                    Task.Run(() => Reader(buffer)),
                    Task.Run(() => Writer(buffer))
                };

                Task.WaitAll(tasks.ToArray());
            },
                               configuration: GetConfiguration().WithTestingIterations(100),
                               errorChecker: (e) =>
            {
                Assert.StartsWith("Deadlock detected.", e);
            },
                               replay: true);
        }
Пример #19
0
 private static void StartComputationThreads(List<long> results, BoundedBuffer<long> numbersToCheck)
 {
     var threads = CreateThreads(results, numbersToCheck);
     threads.ForEach(thread => thread.Start());
 }
Пример #20
0
 public MyConsumer(BoundedBuffer boundedBuffer, int iterations)
 {
     this.boundedBuffer = boundedBuffer;
     totalIters         = iterations;
 }
 public IsNumberPrimeCalculator(ICollection<long> primeNumbers, BoundedBuffer<long> numbersToCheck,Semaphore resultMutex)
 {
     _primeNumbers = primeNumbers;
     _numbersToCheck = numbersToCheck;
     _resultMutex = resultMutex;
 }
Пример #22
0
 public void SetUp()
 {
     boundedBuffer = new BoundedBuffer <int>(2);
     timeout       = new TimeSpan(50);
 }
Пример #23
0
        public void DefaultCtor_ProperlySetsMaxSize()
        {
            var buffer = new BoundedBuffer <int>();

            Assert.AreEqual(100, buffer.MaxSize);
        }
Пример #24
0
 /**
  * Returns a synchronized buffer backed by the given buffer that will
  * block on {@link Buffer#add(Object)} and
  * {@link Buffer#addAll(java.util.Collection)} until enough object(s) are
  * removed from the buffer to allow the object(s) to be added and still
  * maintain the maximum size.
  *
  * @param buffer  the buffer to make bounded,  must not be null
  * @param maximumSize  the maximum size
  * @return a bounded buffer backed by the given buffer
  * @throws IllegalArgumentException if the given buffer is null
  * @since Commons Collections 3.2
  */
 public static Buffer boundedBuffer(Buffer buffer, int maximumSize)
 {
     return(BoundedBuffer.decorate(buffer, maximumSize));
 }
Пример #25
0
 public BWorker()
 {
     _buf = new BoundedBuffer <Bottle>();
     _sem = new Semaphore(1, 1);
 }
Пример #26
0
 /**
  * Returns a synchronized buffer backed by the given buffer that will
  * block on {@link Buffer#add(Object)} and
  * {@link Buffer#addAll(java.util.Collection)} until enough object(s) are
  * removed from the buffer to allow the object(s) to be added and still
  * maintain the maximum size or the timeout expires.
  *
  * @param buffer the buffer to make bounded, must not be null
  * @param maximumSize the maximum size
  * @param timeoutMillis  the timeout value in milliseconds, zero or less for no timeout
  * @return a bounded buffer backed by the given buffer
  * @throws IllegalArgumentException if the given buffer is null
  * @since Commons Collections 3.2
  */
 public static Buffer boundedBuffer(Buffer buffer, int maximumSize, long timeoutMillis)
 {
     return(BoundedBuffer.decorate(buffer, maximumSize, timeoutMillis));
 }