A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 靳石磊 中级黑马   /  2013-4-12 22:00  /  1096 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 靳石磊 于 2013-4-12 22:03 编辑

两个线程执行同一个方法改变同一个值:
class ThreadUse
    {
        public int count = 0;        
        public void CountAdd()
        {
            for (int i = 1; i <= 100; i++)
            {
                Console.WriteLine(Thread.CurrentThread.Name.ToString());
                this.count++;
                Console.WriteLine("i=" + this.count);     
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {            
            ThreadUse threaduse = new ThreadUse();
            Thread thread2 = new Thread(threaduse.CountAdd);
            thread2.Name = "线程2";
            Thread thread3 = new Thread(threaduse.CountAdd);
            thread3.Name = "线程3";
            thread2.Start();
            thread2.Join();
            thread3.Start();
            thread3.Join();
            //thread2.Abort();
            //thread3.Abort();
            Console.WriteLine(threaduse.count);
            Console.ReadKey();
        }        
    }
执行结果:

为什么i是从52开始的,i从1到50为什么没有输出?

C:\Users\Administrator\Desktop\未命名.jpg

评分

参与人数 1技术分 +1 收起 理由
苏波 + 1

查看全部评分

4 个回复

倒序浏览
  1.     class ThreadUse
  2.     {
  3.         public int count = 0;
  4.         public void CountAdd()
  5.         {
  6.             for (int i = 1; i <= 100; i++)
  7.             {
  8.                     Console.WriteLine(count++);
  9.             }
  10.         }
  11.     }
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             ThreadUse threaduse = new ThreadUse();
  17.             Thread thread2 = new Thread(threaduse.CountAdd);
  18.             thread2.Name = "线程2";
  19.             Thread thread3 = new Thread(threaduse.CountAdd);
  20.             thread3.Name = "线程3";
  21.             thread2.Start();
  22.             thread3.Start();
  23.             //thread2.Abort();
  24.             //thread3.Abort();
  25.             //Console.WriteLine(threaduse.count);
  26.             Console.ReadKey();
  27.         }
  28.     }
复制代码
回复 使用道具 举报
曾玉锋 发表于 2013-4-12 23:38

为什么有这一句 Console.WriteLine(Thread.CurrentThread.Name.ToString());
和没有这一句会有区别,原因可在?
回复 使用道具 举报
本帖最后由 崔宏奎 于 2013-4-17 22:18 编辑

目测这样会出错的,某些时候会打印出i=101;

Ps:往上翻翻看看,不应该没执行
回复 使用道具 举报
  1. class ThreadUse
  2. {
  3. public int count = 0;
  4. //定义一个对象用做锁
  5. ThreadUser user=new ThreadUser();
  6. public void CountAdd()
  7. {
  8. //锁住线程,只允许挨个访问,不允许多个线程同时访问
  9. lock(user){
  10. for (int i = 1; i <= 100; i++)
  11. {
  12. Console.WriteLine(count++);
  13. }
  14. }
  15. }
  16. }
  17. class Program
  18. {
  19. static void Main(string[] args)
  20. {
  21. ThreadUse threaduse = new ThreadUse();
  22. Thread thread2 = new Thread(threaduse.CountAdd);
  23. thread2.Name = "线程2";
  24. Thread thread3 = new Thread(threaduse.CountAdd);
  25. thread3.Name = "线程3";
  26. thread2.Start();
  27. thread3.Start();
  28. //thread2.Abort();
  29. //thread3.Abort();
  30. //Console.WriteLine(threaduse.count);
  31. Console.ReadKey();
  32. }
  33. }
复制代码
其实,线程同步涉及到锁的概念。因为你们以后才会接触到,现在说到这里可能会很迷茫
多线程的执行顺序是不可预测的,多执行一行代码和少执行一行代码 结果当然不一样。

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马