Ejemplo n.º 1
0
    public static void Main(string[] args)
    {
        int    count = 0;
        object L     = new object();

        new Thread(() => {
            System.Threading.Thread.Sleep(4000);
            Environment.Exit(0);
        }).Start();

        while (true)
        {
            lock (L) {
                while (count > 10)
                {
                    Monitor.Wait(L);
                }
            }

            if ((R.nextInt() & 1) != 0)
            {
                lock (L) {
                    count++;
                }
                new Thread(() => {
                    Delay();
                    Baboon.onRope();
                    Output("Baboon on rope");
                    Delay();
                    MainClass.Output("Baboon off rope");
                    Baboon.offRope();
                    lock (L){
                        count--;
                        Monitor.Pulse(L);
                    }
                }).Start();
            }
            else
            {
                lock (L) {
                    count++;
                }
                new Thread(() => {
                    Delay();
                    Macaque.onRope();
                    Output("Macaque on rope");
                    Delay();
                    MainClass.Output("Macaque off rope");
                    Macaque.offRope();
                    lock (L){
                        count--;
                        Monitor.Pulse(L);
                    }
                }).Start();
            }
        }
    }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            IHuman human1 = new Man();
            IHuman human2 = new Woman();

            IMonkey monkey1      = new Baboon();
            IMonkey monkey2      = new Chimpanzee();
            IMonkey monkey3      = new Gorilla();
            IMonkey humanMonkey1 = new HumanAdapter(human1);
            IMonkey humanMonkey2 = new HumanAdapter(human2);

            monkey1.Screech();
            monkey2.Screech();
            monkey3.Screech();
            humanMonkey1.Screech();
            humanMonkey2.Screech();
        }