예제 #1
0
        public static void Main()
        {
            ReentrantLock _lock = new ReentrantLock();
            Condition     cond  = _lock.NewCondition();

            Thread c = new Thread(() =>
            {
                _lock.Lock();
                cond.Wait();
                Console.WriteLine("AQUI ");
                _lock.Unlock();
            });

            Thread l = new Thread(() =>
            {
                Thread.Sleep(1000);
                _lock.Lock();
                cond.Pulse();
                _lock.Unlock();
            });

            c.Start();
            l.Start();
            Thread.Sleep(5000);
            Console.WriteLine("Pilihla");
        }
예제 #2
0
 public void P(int units)
 {
     try
     {
         _lock.Lock();
         if (_units >= units)
         {
             _units -= units;
             return;
         }
         units -= _units;
         _units = 0;
         Request myRequest = new Request();
         myRequest._missingUnits = units;
         myRequest._condition    = new Condition(_lock);
         _requeue.AddLast(myRequest);
         while (true)
         {
             myRequest._condition.Wait();
             if (myRequest._missingUnits == 0)
             {
                 return;
             }
         }
     }
     finally
     {
         _lock.Unlock();
     }
 }