public static void Main(String[] args) { Thread[] threads = new Thread[10]; Account acc = new Account(1000); for (int i = 0; i < 10; i++) { Thread t = new Thread(new ThreadStart(acc.DoTransactions)); threads[i] = t; } for (int i = 0; i < 10; i++) { threads[i].Start(); } //Other Programs Code ///////////////////////////////////////////////////////////////////////////////////////////////////// int result = 0; // Result initialized to say there is no error Cell cell = new Cell(); CellProd prod = new CellProd(cell, 20); // Use cell for storage, // produce 20 items CellCons cons = new CellCons(cell, 20); // Use cell for storage, // consume 20 items Thread producer = new Thread(new ThreadStart(prod.ThreadRun)); Thread consumer = new Thread(new ThreadStart(cons.ThreadRun)); // Threads producer and consumer have been created, // but not started at this point. try { producer.Start(); consumer.Start(); //Join waits for a thread to finish. On the Thread type, we find the Join instance method. //It enables us to wait until the thread finishes. We use the Join method on an array of threads //to implement useful threading functionality. producer.Join(); // Join both threads with no timeout // Run both until done. consumer.Join(); // threads producer and consumer have finished at this point. } catch (ThreadStateException e) { Console.WriteLine(e); // Display text of exception result = 1; // Result says there was an error } catch (ThreadInterruptedException e) { Console.WriteLine(e); // This exception means that the thread // was interrupted during a Wait result = 1; // Result says there was an error } // Even though Main returns void, this provides a return code to // the parent process. Environment.ExitCode = result; }
public static void Main(String[] args) { int result = 0; // Result initialized to say there is no error Cell cell = new Cell(); CellProd prod = new CellProd(cell, 50, 2, "Production"); // Use cell for storage, // produce 20 items CellCons cons = new CellCons(cell, 21, 3, "Consumer 1"); // Use cell for storage, // consume 20 items CellCons cons2 = new CellCons(cell, 10, 1, "Consumer 2"); // Use cell for storage, // consume 20 items Thread producer = new Thread(new ThreadStart(prod.ThreadRun)); Thread consumer = new Thread(new ThreadStart(cons.ThreadRun)); Thread consumer2 = new Thread(new ThreadStart(cons2.ThreadRun)); // Threads producer and consumer have been created, // but not started at this point. try { producer.Start(); consumer.Start(); consumer2.Start(); producer.Join(); // Join both threads with no timeout // Run both until done. consumer.Join(); consumer2.Join(); // threads producer and consumer have finished at this point. } catch (ThreadStateException e) { Console.WriteLine(e); // Display text of exception result = 1; // Result says there was an error } catch (ThreadInterruptedException e) { Console.WriteLine(e); // This exception means that the thread // was interrupted during a Wait result = 1; // Result says there was an error } // Even though Main returns void, this provides a return code to // the parent process. Environment.ExitCode = result; Console.ReadLine(); }