예제 #1
0
        public void BossWorkerDemo()
        {
            int n = 10;

            Latch     startPermit = new Latch();
            Semaphore wait        = new Semaphore(-(n - 1));

            Worker [] workers = new Worker[n];
            for (int i = 0; i < n; ++i)
            {
                workers[i] = new Worker(startPermit, wait);
                new Thread(new ThreadStart(workers[i].Work)).Start();
            }
            // very slow main initialization ...
            // ... parse configuration
            // ... initialize other resources used by workers
            startPermit.Release();

            // now it is our turn to wait for workers
            wait.Acquire();

            for (int i = 0; i < n; ++i)
            {
                Assert.IsTrue(workers[i].worked);
            }
        }
 public void CanBeInterruptedAgainAfterInterruption()
 {
     Latch stop = new Latch ();
     Latch go = new Latch ();
     InterruptAgainStrategy strategy = new InterruptAgainStrategy (go, stop);
     Runner runner = new Runner (strategy);
     Thread t = StartedThread (runner);
     Log ("Thread #" + t.GetHashCode () + " started: interrupting it");
     t.Interrupt ();
     Log ("Thread #" + t.GetHashCode () + " interrupted");
     runner.Interrupted ();
     Log ("artificially signalled interruption");
     go.Acquire ();
     t.Interrupt ();
     stop.Release ();
     Log ("Joining thread #" + t.GetHashCode ());
     t.Join ();
     Assert.IsTrue (strategy.interrupted, "thread could not take any action after interruption");
     Assert.IsTrue (strategy.reInterrupted, "thread could not interrupt itself after interruption");
 }
예제 #3
0
        public void CanBeInterruptedAgainAfterInterruption()
        {
            Latch stop = new Latch();
            Latch go   = new Latch();
            InterruptAgainStrategy strategy = new InterruptAgainStrategy(go, stop);
            Runner runner = new Runner(strategy);
            Thread t      = StartedThread(runner);

            Log("Thread #" + t.GetHashCode() + " started: interrupting it");
            t.Interrupt();
            Log("Thread #" + t.GetHashCode() + " interrupted");
            runner.Interrupted();
            Log("artificially signalled interruption");
            go.Acquire();
            t.Interrupt();
            stop.Release();
            Log("Joining thread #" + t.GetHashCode());
            t.Join();
            Assert.IsTrue(strategy.interrupted, "thread could not take any action after interruption");
            Assert.IsTrue(strategy.reInterrupted, "thread could not interrupt itself after interruption");
        }
예제 #4
0
        public void BossWorkerDemo() 
        {
            int n = 10;

            Latch startPermit = new Latch();
            Semaphore wait = new Semaphore(-(n - 1));
            
            Worker [] workers = new Worker[n];
            for (int i=0; i<n; ++i) 
            {
                workers[i] = new Worker(startPermit, wait);
                new Thread(new ThreadStart(workers[i].Work)).Start();
            }
            // very slow main initialization ...
            // ... parse configuration
            // ... initialize other resources used by workers
            startPermit.Release();

            // now it is our turn to wait for workers
            wait.Acquire();

            for (int i=0; i<n; ++i) 
                Assert.IsTrue(workers[i].worked);
        }
예제 #5
0
 public void WaitForStartedBeforeStopping()
 {
     MyService serviceable = new MyService(sync1, sync2);
     ISync starting = new Latch();
     ISync started = new Latch();
     BlockWhileStartingExecutor executor = 
         new BlockWhileStartingExecutor(starting, started);
     MyServiceSupport support = new MyServiceSupport(executor, serviceable);
     executor.ServiceSupport = support;
     Thread startThread = new Thread(new ThreadStart(executor.Start));
     startThread.Name = "start";
     startThread.Start();
     Log ("start thread started");
     Latch stopping = new Latch();
     Latch stopped = new Latch();
     StoppingHelper helper = new StoppingHelper(support, stopping, stopped);
     Thread stopThread = new Thread(new ThreadStart(helper.Stop));
     stopThread.Name = "stop";
     stopThread.Start();
     Log ("stop thread started: waiting for stopping ...");
     stopping.Acquire();
     Log ("stopping in progress ...");
     Assert.IsFalse(executor.wasStarted);
     Assert.IsFalse(helper.wasStopped, "helper could stop before expected");
     Log ("allow to start ...");
     starting.Release();
     Log ("waiting for started ...");
     started.Acquire();
     Assert.IsTrue(executor.wasStarted);
     stopped.Acquire();
     Log ("waiting for stop ...");
     Assert.IsTrue(helper.wasStopped);
     Log ("stopped ...");
 }