ITPub博客

首页 > > > 多线程问题 !

多线程问题 !

作者:abcjob 时间:2019-09-11 13:45:54 0 删除 编辑

自己弄了个例子为什么我开辟的4个线程运行的时间比 普通的方法时间还长呢?

当然你们会说什么调度 时间片 线程的切换的原因要怎么才快呢?

代码

先新建个类ThreadDemo 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadDemo
{
    public class ThreadDemo
    {
        public Thread thread1 = null;
        public Thread thread2 = null;
        public Thread thread3 = null;
        public Thread thread4 = null;

        public List<int> listInt = null;
        private List<int> listInt2 = null;
        private event EventHandler ehRemoveCompleted;

        public ThreadDemo()
        {
            ehRemoveCompleted += new EventHandler(ThreadDemo_ehRemoveCompleted);
            listInt = new List<int>();
            for (int i = 0; i < 10000; i++)
            {
                listInt.Add(i);
            }

            thread1 = new Thread(Run);
            thread1.Name = "线程1";
            thread2 = new Thread(Run);
            thread2.Name = "线程2";
            thread3 = new Thread(Run);
            thread3.Name = "线程3";
            thread4 = new Thread(Run);
            thread4.Name = "线程4";

            listInt2 = new List<int>();
            for (int j = 0; j < 10000; j++)
            {
                listInt2.Add(j);
            }
        }

        void ThreadDemo_ehRemoveCompleted(object sender, EventArgs e)
        {
            Program.methodWatch.Stop();
            Console.WriteLine(Program.methodWatch.ElapsedTicks + "____________________________________");
            thread1.Abort();
            thread2.Abort();
            thread3.Abort();
            thread4.Abort();
        }

        public void DoWorker()
        {
            thread1.Start();
            thread2.Start();
            thread3.Start();
            thread4.Start();
        }

        void Run()
        {
            while (true)
            {
                Monitor.Enter(this);
                if (listInt.Count > 0)
                {
                    listInt.RemoveAt(0);
                }
                Monitor.Exit(this);

                if (listInt.Count == 0)
                {
                    ehRemoveCompleted(this, new EventArgs());
                }
            }
        }

        public void DoWorker2()
        {
            for (int i = 0; i < listInt2.Count; i++)
            {
                listInt2.Remove(i);
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadDemo
{
    public class ThreadDemo
    {
        public Thread thread1 = null;
        public Thread thread2 = null;
        public Thread thread3 = null;
        public Thread thread4 = null;

        public List<int> listInt = null;
        private List<int> listInt2 = null;
        private event EventHandler ehRemoveCompleted;

        public ThreadDemo()
        {
            ehRemoveCompleted += new EventHandler(ThreadDemo_ehRemoveCompleted);
            listInt = new List<int>();
            for (int i = 0; i < 10000; i++)
            {
                listInt.Add(i);
            }

            thread1 = new Thread(Run);
            thread1.Name = "线程1";
            thread2 = new Thread(Run);
            thread2.Name = "线程2";
            thread3 = new Thread(Run);
            thread3.Name = "线程3";
            thread4 = new Thread(Run);
            thread4.Name = "线程4";

            listInt2 = new List<int>();
            for (int j = 0; j < 10000; j++)
            {
                listInt2.Add(j);
            }
        }

        void ThreadDemo_ehRemoveCompleted(object sender, EventArgs e)
        {
            Program.methodWatch.Stop();
            Console.WriteLine(Program.methodWatch.ElapsedTicks + "____________________________________");
            thread1.Abort();
            thread2.Abort();
            thread3.Abort();
            thread4.Abort();
        }

        public void DoWorker()
        {
            thread1.Start();
            thread2.Start();
            thread3.Start();
            thread4.Start();
        }

        void Run()
        {
            while (true)
            {
                Monitor.Enter(this);
                if (listInt.Count > 0)
                {
                    listInt.RemoveAt(0);
                }
                Monitor.Exit(this);

                if (listInt.Count == 0)
                {
                    ehRemoveCompleted(this, new EventArgs());
                }
            }
        }

        public void DoWorker2()
        {
            for (int i = 0; i < listInt2.Count; i++)
            {
                listInt2.Remove(i);
            }
        }
    }
}

为什么 怎么才能快一点呢?

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4798/viewspace-3162360/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-10-12

  • 博文量
    159
  • 访问量
    6869