コード例 #1
0
    private static void SingleThread()
    {
        Console.WriteLine("\n--- Now doing singlethreading ---");
        Thread.Sleep(1000);

        sw.Start();

        for (int i = 0; i < threadingObjectList.Count; i++)
        {
            ThreadMethods.Method1(threadingObjectList);
            ThreadMethods.Method2(threadingObjectList);
        }

        sw.Stop();
        Console.WriteLine("Time elapsed for singlethreading: " + sw.Elapsed);
        sw.Reset();
    }
コード例 #2
0
    private static void Setup()
    {
        if (threadList.Count > 0 && threadListMutex.Count > 0 && threadListSemaphore.Count > 0)
        {
            threadList.Clear();
            threadListMutex.Clear();
            threadListSemaphore.Clear();
        }

        MutexMethods.counter  = 0;
        MutexMethods.counter2 = 0;

        if (threadingObjectList.Count == 0)
        {
            for (int i = 0; i < 500; i++)
            {
                var myObject = new ThreadingObject("Name" + i, i, true, new List <KeyValuePair <int, string> >());
                threadingObjectList.Add(myObject);
            }
        }

        for (int i = 0; i < 50; i++)
        {
            threadList.Add(new Thread(() => ThreadMethods.Method1(threadingObjectList)));
            threadList.Add(new Thread(() => ThreadMethods.Method2(threadingObjectList)));

            threadListMutex.Add(new Thread(() => MutexMethods.Method1(threadingObjectList)));
            threadListMutex.Add(new Thread(() => MutexMethods.Method2(threadingObjectList)));
        }

        for (int i = 0; i < 10; i++)
        {
            threadListSemaphore.Add(new Thread((SemaphoreMethods.Method1))
            {
                Name = i.ToString()
            });
        }
    }