All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP技巧实例:树形结构的算法_php资料_编程技术
ClassWizard的功能
PHP中 ADODB 类库介绍(一)_[PHP教程]
如何找出相同月日记录_[SQL Server教程]
jsp+ajax自动刷新实例_[AJAX教程]
ASP.NET,2.0页面性能的考虑_[Asp.Net教程]
什么是面向对象编程
C#中ImageAnimator类功能和方法
Asp.net总调用的3个事件_[Asp.Net教程]
用asp.net把Excel转换为SQL,Server_[Asp.Net教程]
Reading number is top 10 pictures
Embarrassing things comic collection2
Kim jong il's mistress, national beauty JinYuJi actor1
Soldier saw beauty after the reaction
Discharge accidentally Actresses by the breast2
2012 national geographic daily picture7
The little woman's bright wire1
Sexy women in 2013--1
这才是真正的人体艺术3
Look for from human art net, is good--3
So beauty, will let you spray blood9
Download software ranking
Detective task-the top secret prostitution files
C#COM编程指南
传奇私服架设教程
Tram sex maniac 2 (H) rar bag6
WebService在.NET中的实战应用教学视频 → 第5集
Boxer Classic video3
dreamweaver8中文版
卡丁车单机版
终极变速大师Speeder3.26
Tram sex maniac 2 (H) rar bag19
delv published in(发表于) 2014/1/6 9:05:12 Edit(编辑)
ASP.NET:一段比较经典的多线程学习代码_[Asp.Net教程]

ASP.NET:一段比较经典的多线程学习代码_[Asp.Net教程]

ASP.NET:一段比较经典的多线程学习代码_[Asp.Net教程]

一段比较经典的多线程学习代码。

  1、用到了多线程的同步问题。
  2、用到了多线程的顺序问题。


  如果有兴趣的请仔细阅读下面的代码。注意其中代码段的顺序,思考一下,这些代码的顺序能否互相调换,为什么?这应该对学习很有帮助的。为了演示,让所有的线程都Sleep了一段时间。


using System.Net;
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Diagnostics;


namespace Webb.Study
{
class TestThread
{
static Mutex m_Mutex = new Mutex();
static Thread[] m_testThreads = new Thread[10];
static int m_threadIndex = 0;


static void ThreadCallBack()
{
TestThread.m_Mutex.WaitOne();
int m_index = m_threadIndex;
TestThread.m_Mutex.ReleaseMutex();
Console.WriteLine("Thread {0} start.",m_index);
for(int i=0;i<=10;i++)
{
TestThread.m_Mutex.WaitOne();
Console.WriteLine("Thread {0}: is running. {1}",m_index,i);
TestThread.m_Mutex.ReleaseMutex();
Thread.Sleep(100);
}
Console.WriteLine("Thread {0} end.",m_index);
}


public static void Main(String[] args)
{
Console.WriteLine("Main thread start.");
for(int i=0;i {
TestThread.m_threadIndex = i;
TestThread.m_testThreads[i] = new Thread(new ThreadStart(ThreadCallBack));
TestThread.m_testThreads[i].Start();
Thread.Sleep(100);
}
for(int i=0;i {
TestThread.m_testThreads[i].Join();
}
Console.WriteLine("Main thread exit.");
}
}
}


  1、主函数中这两句能否互换?为什么?


TestThread.m_testThreads[i].Start();
Thread.Sleep(100);


  2、CallBack函数中这两句能否互换?为什么?会有什么不同的结果?


TestThread.m_Mutex.ReleaseMutex();
Thread.Sleep(100);


  3、主函数能否写成这样?为什么?会有什么不同的结果?


public static void Main(String[] args)
{
Console.WriteLine("Main thread start.");
for(int i=0;i {
TestThread.m_threadIndex = i;
TestThread.m_testThreads[i] = new Thread(new ThreadStart(ThreadCallBack));
TestThread.m_testThreads[i].Start();
TestThread.m_testThreads[i].Join();
Thread.Sleep(100);
}
Console.WriteLine("Main thread exit.");
}


  4、这几句的作用是什么?那么程序中还存在什么样的问题?应该做怎样的修改?


TestThread.m_Mutex.WaitOne();
int m_index = m_threadIndex;
TestThread.m_Mutex.ReleaseMutex();


仅做学习讨论。


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.