Exemplo n.º 1
0
        public double Calc(int factorial, int numThreads)
        {
            // Start at one higher because first thing we do is decrement
            this.unallocated = factorial + 1;

            // Create and start workers
            Worker[] work = new Worker[numThreads];
            Thread[] threads = new Thread[numThreads];

            // create and start threads
            for (long i = 0; i < numThreads; i++)
            {
                work[i] = new Worker(this);
                Thread thread = new Thread(work[i].Start);
                threads[i] = thread;
                thread.Start();
            }

            // wait for threads to finish
            foreach (var thread in threads)
            {
                thread.Join();
            }

            // Multiply the results of workers
            double result = 1;
            for (long i = 0; i < numThreads; i++)
            {
                result *= work[i].total;
            }

            return result;
        }
Exemplo n.º 2
0
 private static void initSocket(String ip, int port)
 {
     worker = new Worker(ip, port);
     Thread workerThread = new Thread(worker.DoWork);
     workerThread.Start();
 }