Esempio n. 1
0
        static void Main(string[] args)
        {
            Philosopher aristotle   = new Philosopher(Table.Plastikowy, Table.Platinowy, "Aristotle", 4);
            Philosopher confucius   = new Philosopher(Table.Platinowy, Table.Zloty, "Confucius", 5);
            Philosopher kant        = new Philosopher(Table.Zloty, Table.Srebny, "Immanuel Kant", 6);
            Philosopher augustine   = new Philosopher(Table.Srebny, Table.Drzewniany, "Augustine", 4);
            Philosopher machiavelli = new Philosopher(Table.Drzewniany, Table.Plastikowy, "Niccolo Machiavelli", 7);

            new Thread(aristotle.Think).Start();
            new Thread(confucius.Think).Start();
            new Thread(kant.Think).Start();
            new Thread(augustine.Think).Start();
            new Thread(machiavelli.Think).Start();

            Console.ReadKey();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Philosopher philos = new Philosopher();
            Thread[] threads = new Thread[Philosopher.TOTAL_PHILOSOPHERS];

            // initialize all threads
            for (int i = 0; i < Philosopher.TOTAL_PHILOSOPHERS; i++)
            {
                threads[i] = new Thread(new ParameterizedThreadStart(philos.run));
                //System.Console.WriteLine("Debugging: THREAD {0} IS BEING CREATED\n", i);
            }

            // start all thread
            for (int i = 0; i < Philosopher.TOTAL_PHILOSOPHERS; i++)
            {
                try
                {
                   // System.Console.WriteLine("Debugging: THREAD {0} IS BEING STARTED\n", i);
                    threads[i].Start(i);    // pass the parameter to the run method
                }
                catch (ThreadStateException e)
                {
                    Console.WriteLine(e);  // Display text of exception
                }
                catch (ThreadInterruptedException e)
                {
                    Console.WriteLine(e);
                }
            }

            // join all thread
            for (int i = 0; i < Philosopher.TOTAL_PHILOSOPHERS; i++)
            {
                try
                {
                    //System.Console.WriteLine("Debugging: THREAD {0} IS BEING JOINED\n", i);
                    threads[i].Join();
                }
                catch (ThreadStateException e)
                {
                    Console.WriteLine(e);  // Display text of exception
                }
                catch (ThreadInterruptedException e)
                {
                    Console.WriteLine(e);
                }
            }

            System.Console.ReadKey();
        }