Exemple #1
0
            /// <summary>
            /// Creates the multiple threads: this example shows:
            /// - creation of multiple threads to be running in parallel.
            /// - passing parameters to the task.
            /// - check the output. Can you predict the pattern of interleaving?
            /// </summary>
            public void createMultipleThreads()
            {
                Console.WriteLine("Press a key to start multiple threads printing separate characters ... ");
                Console.Read();

                int num = 500;

                char[]   chars   = { 'A', 'B', 'C', 'D', 'E' };
                Thread[] threads = new Thread[chars.Length];

                for (int i = 0; i < chars.Length; i++)
                {
                    char c = chars[i];
                    threads[i] = new Thread(() => ThreadCreation.printChars(num, c));
                }

                for (int i = 0; i < chars.Length; i++)
                {
                    Console.WriteLine("{0}", threads[i].ThreadState);
                    threads[i].Start();
                    Console.WriteLine("{0}", threads[i].ThreadState);
                    // Uncomment this to see the change of the states for each thread and its output separately
                    // otherwise the results of the threads will be interleaved
                    //Console.Read();
                }


                // Here the main thread waits for all to finish
                for (int i = 0; i < chars.Length; i++)
                {
                    threads[i].Join();
                }

                Console.WriteLine(" The main thread has terminated ... ");
            }
Exemple #2
0
        private static void ThreadCreationExample()
        {
            var threadCreation = new ThreadCreation();

            threadCreation.UsingThread();

            threadCreation.UsingThreadStart();

            threadCreation.UsingThreadStartWithParameters();

            threadCreation.UsingLambdaExpressions();

            Console.ReadKey();
        }
Exemple #3
0
        public void runExamples()
        {
            ThreadsList tl = new ThreadsList();
            // todo: uncomment this and check the execution
            //tl.runExample();

            ThreadCreation tc = new ThreadCreation();
            // todo: uncomment this and check the execution
            //tc.runExample();

            ThreadsJoin tj = new ThreadsJoin(2000);
            // todo: uncomment this and check the execution
            //tj.runExample();
        }
Exemple #4
0
 public virtual TResult Visit(ThreadCreation e, TData data)
 {
     return(Visit((JdwpEvent)e, data));
 }