贡献100分,可以吸引大牛进来赐教下么?

config_man 2014-05-15 03:42:43

static void Doing(int i)
{
Console.WriteLine(i.ToString());
}

static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
//代码A
Action<int> action = new Action<int>(Doing);
action.BeginInvoke(i, null, null);
}

Console.WriteLine("循环结束...");
Console.ReadKey();
}


想问下,代码A用的是多线程么?循环10次,是生成10个线程么?如果不是,是什么样的情况呢?
另外还想问下,如果把这整块代码用手工写几个Thread来执行(每个Thread处理几个),与现在的这种是一样的么?

怕只怕还没看明白的,就回复。怕只怕类似“Action是委托,BeginInvoke是异步调用”这样的回复了,如果您能回复严谨些,那真是感激了...
...全文
358 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
config_man 2014-05-16
  • 打赏
  • 举报
回复
引用 22 楼 iyomumx 的回复:
[quote=引用 17 楼 config_man 的回复:] 我晕,你放在Doing方法内打印的线程。在Doing内,确实不一样。在for循环内,是一样的。
我认为for循环是线性执行这件事情应该是常识。如果你希望并行执行for循环,请使用Parallel.For方法。但这个时候BeginInvoke就没有必要出现了。[/quote] 单纯for直接打印数字的话,是线性。如果去掉直接打印,改用Action.BeginInvoke,应该属于并行了吧。当然Parallel.For是并行的,这个我用过。我用BeginInvoke的主要目的是想知道,此方法是否为多线程。
config_man 2014-05-16
  • 打赏
  • 举报
回复
最后总结下吧。 9楼iyomumx的仍然可判断出线程的Id,即使没有添加前缀。因为有数字重复,可作为依据。这是我的错误之一。 版主在开头即明确告知,在方法内,加上获取线程Id的方法,我自己疏忽,放在了for循环内。这是我的错误之二。 有两位传道授业解惑的师者给了方法,我盲目的进行测试,没有理性、仔细的进行分析处理。这是我的错误之三。 希望后来者,在看到这篇帖子时,注意下这个总结。也希望自己以后不要再干这样的蠢事。 最后深深的感谢iyomumx,感谢。感谢斑竹在最近几篇帖子中的回复。感谢。
iyomumx 2014-05-16
  • 打赏
  • 举报
回复
引用 17 楼 config_man 的回复:
我晕,你放在Doing方法内打印的线程。在Doing内,确实不一样。在for循环内,是一样的。
我认为for循环是线性执行这件事情应该是常识。如果你希望并行执行for循环,请使用Parallel.For方法。但这个时候BeginInvoke就没有必要出现了。
config_man 2014-05-16
  • 打赏
  • 举报
回复
晕倒。最后发现是我自己的疏忽,理解错了。 版主是让在方法内加的,我自己没看仔细,就放在了for循环内。我的错。我的错。万分抱歉,两位。 今晚这这几个小时,学习了。向两位作揖,拜谢!结贴。
config_man 2014-05-16
  • 打赏
  • 举报
回复
引用 18 楼 caozhy 的回复:
给帖子加了100分。记得结贴的时候全部结给iyomumx。
嗯。多谢iyomumx,多谢版主。iyomumx,在此表示抱歉。你确实是对的。
config_man 2014-05-16
  • 打赏
  • 举报
回复
晕了,被版主忽悠了。。。。 iyomumx是对的,正确的检测方法确实应该放在Doing方法内。 这说明Action.BeginInvoke确实是多线程。
threenewbee 2014-05-16
  • 打赏
  • 举报
回复
给帖子加了100分。记得结贴的时候全部结给iyomumx。
config_man 2014-05-16
  • 打赏
  • 举报
回复
我晕,你放在Doing方法内打印的线程。在Doing内,确实不一样。在for循环内,是一样的。
threenewbee 2014-05-16
  • 打赏
  • 举报
回复
谢谢iyomumx,也谢谢lz的问题。
config_man 2014-05-16
  • 打赏
  • 举报
回复
Console.WriteLine(Thread.CurrentThread.ManagedThreadId); 当前线程id碰巧在1-10之间,正好跟打印i的值(也在1-10之间)混在一起,你还怎么区分哪个是线程的id? 但是你把它换成:Console.WriteLine(“aaa : ”+Thread.CurrentThread.ManagedThreadId); 即使你循环10000次,仍然是同一个。
threenewbee 2014-05-16
  • 打赏
  • 举报
回复
失误失误,抱歉,不过我说了,以程序为准。
iyomumx 2014-05-16
  • 打赏
  • 举报
回复
引用 11 楼 config_man 的回复:
[quote=引用 9 楼 iyomumx 的回复:] 我就呵呵了,敢不敢贴代码
using System;
using System.IO;
using System.Threading;
using System.Linq;

public static class Program
{
	static void Doing(int i)
	{
		Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
		Console.WriteLine(i.ToString());
	}
    public static void Main(String[] args)
    {
        try
        {
            for (int i = 0; i < 10; i++)
            {
                //代码A
                Action<int> action = new Action<int>(Doing);
                action.BeginInvoke(i, null, null);
            }
  
            Console.WriteLine("循环结束...");
            Console.ReadKey();
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine(ex.ToString());
        }
    }
}
输出
引用
循环结束... 3 4 1 0 4 2 3 3 3 5 3 6 3 4 4 4 8 4 9 7
自己看MSDN怎么说:使用异步方式调用同步方法
大哥,你试试再打印线程那行前面随便加些东西,比如aaa之类的,然后拼接当前线程Id试试。[/quote]
引用
循环结束... ID3 0 ID4 1 ID4 ID3 2 ID3 4 ID3 5 ID3 6 ID3 7 ID3 8 3 ID3 9
你想表达什么? 另外,主线程也就是执行For循环的线程一般是ID1
config_man 2014-05-16
  • 打赏
  • 举报
回复
引用 10 楼 caozhy 的回复:
[quote=引用 9 楼 iyomumx 的回复:] 自己看MSDN怎么说:使用异步方式调用同步方法
赞。以程序为准。[/quote] 版主,你怎么搞乌龙了。
config_man 2014-05-16
  • 打赏
  • 举报
回复
引用 9 楼 iyomumx 的回复:
我就呵呵了,敢不敢贴代码
using System;
using System.IO;
using System.Threading;
using System.Linq;

public static class Program
{
	static void Doing(int i)
	{
		Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
		Console.WriteLine(i.ToString());
	}
    public static void Main(String[] args)
    {
        try
        {
            for (int i = 0; i < 10; i++)
            {
                //代码A
                Action<int> action = new Action<int>(Doing);
                action.BeginInvoke(i, null, null);
            }
  
            Console.WriteLine("循环结束...");
            Console.ReadKey();
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine(ex.ToString());
        }
    }
}
输出
引用
循环结束... 3 4 1 0 4 2 3 3 3 5 3 6 3 4 4 4 8 4 9 7
自己看MSDN怎么说:使用异步方式调用同步方法
大哥,你试试再打印线程那行前面随便加些东西,比如aaa之类的,然后拼接当前线程Id试试。
threenewbee 2014-05-16
  • 打赏
  • 举报
回复
引用 9 楼 iyomumx 的回复:
我就呵呵了,敢不敢贴代码
using System;
using System.IO;
using System.Threading;
using System.Linq;

public static class Program
{
	static void Doing(int i)
	{
		Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
		Console.WriteLine(i.ToString());
	}
    public static void Main(String[] args)
    {
        try
        {
            for (int i = 0; i < 10; i++)
            {
                //代码A
                Action<int> action = new Action<int>(Doing);
                action.BeginInvoke(i, null, null);
            }
  
            Console.WriteLine("循环结束...");
            Console.ReadKey();
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine(ex.ToString());
        }
    }
}
输出
引用
循环结束... 3 4 1 0 4 2 3 3 3 5 3 6 3 4 4 4 8 4 9 7
自己看MSDN怎么说:使用异步方式调用同步方法
赞。以程序为准。
iyomumx 2014-05-16
  • 打赏
  • 举报
回复
我就呵呵了,敢不敢贴代码
using System;
using System.IO;
using System.Threading;
using System.Linq;

public static class Program
{
	static void Doing(int i)
	{
		Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
		Console.WriteLine(i.ToString());
	}
    public static void Main(String[] args)
    {
        try
        {
            for (int i = 0; i < 10; i++)
            {
                //代码A
                Action<int> action = new Action<int>(Doing);
                action.BeginInvoke(i, null, null);
            }
  
            Console.WriteLine("循环结束...");
            Console.ReadKey();
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine(ex.ToString());
        }
    }
}
输出
引用
循环结束... 3 4 1 0 4 2 3 3 3 5 3 6 3 4 4 4 8 4 9 7
自己看MSDN怎么说:使用异步方式调用同步方法
Adam_Bryant 2014-05-16
  • 打赏
  • 举报
回复
用委托做的异步是交给线程池处理,肯定是多线程,不过你循环十次样本太小,根本不会开到十个工作线程,线程池已经轻松搞定了。线程池的只有当接收到新任务,且所有线程都在阻塞或者忙碌时才会激活新线程。 手动写10个Thread和扔十个任务跟线程池其实执行过程是不一样的,因为线程池线程都是默认后台线程,且有任务队列调配。
风吹腚腚凉 2014-05-16
  • 打赏
  • 举报
回复
引用 19 楼 config_man 的回复:
晕了,被版主忽悠了。。。。 iyomumx是对的,正确的检测方法确实应该放在Doing方法内。 这说明Action.BeginInvoke确实是多线程。
用屁股想想都知道了,如果不是多线程那么这个方法存在的意义是什么呢?
  • 打赏
  • 举报
回复
  static void Doing(int i)         {             Console.WriteLine(i.ToString());         } 里面可以加个sleep 随机阻塞几百ms 这样玩起多线程 才会清楚一点.
config_man 2014-05-16
  • 打赏
  • 举报
回复
24楼的问题,关于异步和并发、并行,这个不好说。作罢。结。
加载更多回复(8)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧