コード例 #1
0
        static void Main(string[] args)
        {
            int darts;
            int threads;

            Console.WriteLine("Hello! We are going to determine Pi by throwing imaginary darts at a board.");
            Console.WriteLine("Or at least, a quarter of the board.");
            Console.WriteLine();

            Console.Write("How many darts per thread will we throw? ");
            darts = Convert.ToInt32(Console.ReadLine());
            Console.Write("How many Threads will there be? ");
            threads = Convert.ToInt32(Console.ReadLine());

            List <Thread>       myThreads = new List <Thread>();
            List <FindPiThread> myObjs    = new List <FindPiThread>();

            for (int i = 0; i < threads; i++)
            {
                FindPiThread myPi = new FindPiThread(darts);
                myObjs.Add(myPi);

                myThreads.Add(new Thread(new ThreadStart(myPi.throwDarts)));

                myThreads[i].Start();

                Thread.Sleep(16);
            }

            for (int i = 0; i < threads; i++)
            {
                myThreads[i].Join();
            }

            int totalHits = 0;

            for (int i = 0; i < threads; i++)
            {
                totalHits += myObjs[i].dartsLanded;
            }

            Console.WriteLine(totalHits);
            Console.WriteLine(darts);
            Console.WriteLine(threads);

            //This is not clean, but it made it work. And I will take that all day
            double pi = (4 * ((double)totalHits / ((double)darts * (double)threads)));

            Console.WriteLine();
            Console.WriteLine("My estimation of Pi is: " + pi);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: jenrhues/Threading
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Jennifer's Amazing Pi Calculator!\nHere, you will throw darts at 1/4 of a dart board" +
                              " for a specified number of threads to calculate Pi.\nHow many darts would you like to throw?");
            int dartTotal = Int32.Parse(Console.ReadLine());

            Console.WriteLine("How many threads would you like to run?");
            int threadTotal = Int32.Parse(Console.ReadLine());

            //initializes threads
            List <Thread>       thread       = new List <Thread>();
            List <FindPiThread> findPiThread = new List <FindPiThread>();

            Console.WriteLine("Running calculations...\n");

            //sets up threads and starts them
            for (int i = 0; i < threadTotal; i++)
            {
                FindPiThread piSimulation = new FindPiThread(dartTotal);
                findPiThread.Add(piSimulation);
                Thread piThread = new Thread(new ThreadStart(piSimulation.throwDarts));
                thread.Add(piThread);
                piThread.Start();
                Thread.Sleep(16); //pauses Main() for 16ms
            }

            //forces Main() to wait until each thread is completed
            for (int i = 0; i < thread.Count; i++)
            {
                thread[i].Join();
            }

            //adds total dart count inside circle from each thread
            int dartCount = 0;

            for (int i = 0; i < findPiThread.Count; i++)
            {
                dartCount += findPiThread[i].GetDartCount();
            }

            //calculates pi and prints to console
            double Pi = 4.0 * (dartCount / Convert.ToDouble(dartTotal * threadTotal));

            Console.WriteLine("Your Pi estimation is " + Pi + " for " + dartTotal + " darts in " + threadTotal + " threads!\n");

            //makes sure console does not automatically close
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(true);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: kevjpate/Threading
        static void Main(string[] args)
        {
            Console.WriteLine("How many throws should be used?");

            string throws   = Console.ReadLine();
            int    decision = Convert.ToInt32(throws);

            Console.WriteLine("How many threads should be run on?");

            string number  = Console.ReadLine();
            int    threads = Convert.ToInt32(number);

            List <Thread>       threading = new List <Thread>();
            List <FindPiThread> FPT       = new List <FindPiThread>();

            for (int i = 0; i < threads; i++)
            {
                FindPiThread PiT = new FindPiThread(decision);

                FPT.Add(PiT);

                Thread myThread = new Thread(new ThreadStart(PiT.throwDarts));

                threading.Add(myThread);

                myThread.Start();

                Thread.Sleep(16);
            }

            int totalIn = 0;

            for (int k = 0; k < threads; k++)
            {
                threading[k].Join();
            }

            Console.WriteLine("Darts thrown Inside for each thread:");
            for (int j = 0; j < threads; j++)
            {
                Console.WriteLine("Thread Number " + (j + 1) + " " + FPT[j].DThrown);
                totalIn += FPT[j].DThrown;
            }


            Console.WriteLine((4 * ((double)totalIn / ((double)decision * (double)threads))));

            Console.ReadKey();
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: P4Jammies/Threading
        static void Main(string[] args)
        {
            int darts, threads, hits;

            Console.WriteLine("How many threads would you like to open?");
            threads = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("How many darts would you like to throw in each thread?");
            darts = Convert.ToInt32(Console.ReadLine());

            List <Thread>       threadList = new List <Thread>(threads);
            List <FindPiThread> piThreads  = new List <FindPiThread>(threads);

            var calctime = new Stopwatch();

            calctime.Start();

            for (int i = 0; i < threads; i++)
            {
                FindPiThread piThread = new FindPiThread(darts);
                piThreads.Add(piThread);

                Thread thread = new Thread(new ThreadStart(piThread.throwDarts));
                threadList.Add(thread);
                thread.Start();

                Thread.Sleep(16);
            }

            foreach (Thread thread in threadList)
            {
                thread.Join();
            }

            hits = 0;
            foreach (FindPiThread piThread in piThreads)
            {
                hits += piThread.target();
            }

            double pi = 4 * (hits / (double)(darts * threads));

            calctime.Stop();

            Console.WriteLine($"Took {calctime.ElapsedMilliseconds} ms to calculate pi: {pi}\n");
            Thread.Sleep(2000);
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: jorrtayl/CW-9---Threading
        static void Main(string[] args)
        {
            int throwNum    = 0; // throws to make
            int threadNum   = 0; // threads to run
            int dartsInside = 0;

            List <Thread>       threads  = new List <Thread>();
            List <FindPiThread> piThread = new List <FindPiThread>();

            Console.WriteLine("Enter how many throws to make ");
            throwNum = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter how many threads ");
            threadNum = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < threadNum; i++)
            {
                FindPiThread pi = new FindPiThread(throwNum);
                piThread.Add(pi);

                Thread thread = new Thread(new ThreadStart(pi.throwDarts));
                threads.Add(thread);
                thread.Start();
                Thread.Sleep(16);
            }
            foreach (Thread thread in threads)
            {
                thread.Join();
            }
            foreach (FindPiThread thread in piThread)
            {
                dartsInside += thread.dartsLanded;
            }

            double total = throwNum * threadNum; // this is to help make the console output a double

            Console.WriteLine(4 * (dartsInside / total));

            Console.ReadKey();
        }
コード例 #6
0
        static void Main(string[] args)
        {
            int dartsLanded = 0;
            int totalDarts  = 0;

            Console.WriteLine("How many darts will be thrown each thread?");
            int dartsPerThread = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("How many threads will you want?");
            int threadCount = Convert.ToInt32(Console.ReadLine());

            //Creating the threads
            List <Thread>       threadList = new List <Thread>(threadCount);
            List <FindPiThread> piList     = new List <FindPiThread>(threadCount);

            for (int i = 0; i < threadCount; i++)
            {
                FindPiThread piThread = new FindPiThread(dartsPerThread);
                piList.Add(piThread);
                Thread myThread = new Thread(new ThreadStart(piThread.throwDarts));
                threadList.Add(myThread);
                myThread.Start();
                Thread.Sleep(16);
            }
            for (int i = 0; i < threadList.Count; i++)
            {
                threadList[i].Join();
            }
            for (int i = 0; i < piList.Count; i++)
            {
                dartsLanded += piList[i].dartsLandedAccessor;
            }
            totalDarts = dartsPerThread * threadCount;
            double pi = 4.0 * ((double)dartsLanded / (double)totalDarts);

            Console.WriteLine("The number of darts in the target are: " + dartsLanded);
            Console.WriteLine("The total number of darts were: " + totalDarts);
            Console.WriteLine("Pi is: " + pi);
        }
コード例 #7
0
ファイル: Program.cs プロジェクト: Bearden3/Threading
        static void Main(string[] args)
        {
            int darts, threads, accesor = 0;

            Console.WriteLine("How many throws would you like? ");
            darts = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("How many threads would you like?");
            threads = Convert.ToInt32(Console.ReadLine());
            List <Thread>       thread    = new List <Thread>();
            List <FindPiThread> piThreads = new List <FindPiThread>();

            for (int i = 0; i < threads; i++)
            {
                FindPiThread pi = new FindPiThread(darts);
                piThreads.Add(pi);
                Thread StartThread = (new Thread(new ThreadStart(pi.throwDarts)));
                thread.Add(StartThread);
                StartThread.Start();
                Thread.Sleep(16);
            }
            foreach (var j in thread)
            {
                j.Join();
            }
            foreach (var j in piThreads)
            {
                accesor += j.goodDarts;
            }

            Console.WriteLine(accesor);
            decimal total = Convert.ToDecimal(darts * threads);
            decimal good  = Convert.ToDecimal(accesor);
            decimal final = (4 * (good) / (total));

            Console.WriteLine(final);
            Console.ReadKey();
        }
コード例 #8
0
        static void Main(string[] args)
        {
            int                 throws, numThreads;
            int                 inside  = 0;
            List <Thread>       threads = new List <Thread>();
            List <FindPiThread> darts   = new List <FindPiThread>();

            Console.WriteLine("How many throws per thread? ");
            throws = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("How many threads? ");
            numThreads = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < numThreads; i++)
            {
                FindPiThread temp = new FindPiThread(throws);

                darts.Add(temp);
                threads.Add(new Thread(new ThreadStart(darts[i].throwDarts)));
                threads[i].Start();     // Start each thread
                Thread.Sleep(16);       // Sleep main so the random numbers will have a different seed
            }

            for (int i = 0; i < numThreads; i++)
            {
                threads[i].Join();      // Wait for all threads to finish
            }

            for (int i = 0; i < numThreads; i++)
            {
                inside += darts[i].GetDarts();
            }

            Console.WriteLine("Best estimate for Pi: {0}", (4.0 * (inside) / (throws * numThreads)));
            Console.Read();
        }