예제 #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (is_number_valid(sender, e))
            {
                int  length          = 1000000;
                int  ElementToSearch = Convert.ToInt32(textBox1.Text);
                bool flag            = false;

                Stopwatch sw = new Stopwatch();
                sw.Start();
                for (int i = 0; i < length; i++)
                {
                    if (randArray[i] == ElementToSearch)
                    {
                        sw.Stop();
                        textBox2.Text = Convert.ToString(sw.Elapsed);
                        label4.Text   = "at index " + Convert.ToString(i);
                        flag          = true;
                    }
                }
                if (!flag)
                {
                    textBox2.Text = "Not Found";
                    label4.Text   = "";
                    textBox3.Text = "Not Found";
                    label5.Text   = "";
                }

                if (flag)
                {
                    int num_threads = 5;
                    ParameterizedThreadStart parameterizedThread = new ParameterizedThreadStart(threadsearching);
                    Thread[] threads = new Thread[num_threads];
                    sw.Reset();
                    sw.Start();
                    Monitor.Enter(randArray);
                    for (int i = 0; i < num_threads; i++)
                    {
                        threads[i] = new Thread(parameterizedThread);
                        object threadsearch = new ThreadSearch(i, i * threadsize, (i * threadsize) + threadsize, ElementToSearch);
                        threads[i].Start(threadsearch);
                    }
                    Monitor.Exit(randArray);
                    sw.Stop();
                    for (int i = 0; i < num_threads; i++)
                    {
                        threads[i].Join();
                    }

                    textBox3.Text = Convert.ToString(sw.Elapsed);
                    label5.Text   = label4.Text;
                }
            }
        }
예제 #2
0
        public void threadsearching(object param)
        {
            ThreadSearch ts = (ThreadSearch)param;

            for (int i = ts.startIndex; i < ts.endIndex; i++)
            {
                if (randArray[i] == ts.key)
                {
                    positions.Add(i);
                }
            }
        }