多线程与异步的区别和联系
http://netstart.caotingna.com/item/78062.aspx
异步操作是要依靠多线程来实现的。
举个例子在HttpWebRequest类里,如果你在主线程里调用BeginGetResponse方法来请求Internet上的资源,那么系统会自动从系统线程池里另取一个线程,去获取Internet上的资源,当你再调用EndGetResponse方法时,就返回获取的响应!
异步主要是指通信,相对于同步而言。同步通信时,应用程序等待对方通信结束后返回消息循环;异步通信时,不等待对方回应,直接返回到消息循环,当对方回应时,操作系统以事件方式通知应用程序,此间当然也可能处理其它事件。它与线程的主要区别在于,在处理通信的同时不能同时处理其它事件。
如果是两个线程的话,他们的任务可以同时进行,互不影响。
异步当然是多线程,只是线程是由系统来管理的
异步操作一般用于调用比较费时的方法或多人私聊等程序,开始调用后,系统可以继续进行其它操作,等调用方法结束后,使用委托回调函数通知来对已经处理完的方法进行相应处理。
举例:
离你很远的地方有个很漂亮的女网友,你想知道她到底对你如何,你通过邮政礼仪,寄给她一支玫瑰(相当于异步调用)。因为回执要等很多天,所以,你这些天不必整天傻等回执,可以照样吃饭工作(处理些简单的事务),当然,如果你这些天又泡到个MM,这时你就要选择,如果也用异步的方法,当然没问题,因为哪个回执先回来,先处理那个,但如果你对后面这个MM采用同步方式,那你只能解决掉这个同步的MM后,才能处理前面那个回执,不管那个回执何时到达。
这和多线程也有点不同,多线程是两个色狼泡两个MM,自己泡自己的。
嘿嘿
5楼
异步操作是要依靠多线程来实现的。
举个例子在HttpWebRequest类里,如果你在主线程里调用BeginGetResponse方法来请求Internet上的资源,那么系统会自动从系统线程池里另取一个线程,去获取Internet上的资源,当你再调用EndGetResponse方法时,就返回获取的响应!
17楼
异步主要是指通信,相对于同步而言。同步通信时,应用程序等待对方通信结束后返回消息循环;异步通信时,不等待对方回应,直接返回到消息循环,当对方回应时,操作系统以事件方式通知应用程序,此间当然也可能处理其它事件。它与线程的主要区别在于,在处理通信的同时不能同时处理其它事件。
如果是两个线程的话,他们的任务可以同时进行,互不影响。
23楼
异步当然是多线程,只是线程是由系统来管理的
异步操作一般用于调用比较费时的方法或多人私聊等程序,开始调用后,系统可以继续进行其它操作,等调用方法结束后,使用委托回调函数通知来对已经处理完的方法进行相应处理。
举例:
离你很远的地方有个很漂亮的女网友,你想知道她到底对你如何,你通过邮政礼仪,寄给她一支玫瑰(相当于异步调用)。因为回执要等很多天,所以,你这些天不必整天傻等回执,可以照样吃饭工作(处理些简单的事务),当然,如果你这些天又泡到个MM,这时你就要选择,如果也用异步的方法,当然没问题,因为哪个回执先回来,先处理那个,但如果你对后面这个MM采用同步方式,那你只能解决掉这个同步的MM后,才能处理前面那个回执,不管那个回执何时到达。
这和多线程也有点不同,多线程是两个色狼泡两个MM,自己泡自己的。
嘿嘿