|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
因为各系统的API不同,代码调用API编写程序就会遇到很多不兼容的地方,比如Java改写后的Serv-U就不能在手机上执行,手机的游戏也不能直接在微机上执行。编程|多线程- 看一下以下两个例子的运转了局:
- //TestThread.cs
- usingSystem;usingSystem.Threading;publicclassTest{staticintcount=0;staticvoidMain(){ThreadStartjob=newThreadStart(ThreadJob);Threadthread=newThread(job);thread.Start();for(inti=0;i<5;i++){count++;}thread.Join();Console.WriteLine("Finalcount:{0}",count);}staticvoidThreadJob(){for(inti=0;i<5;i++){count++;}}}
复制代码- [code]usingSystem;usingSystem.Threading;publicclassTest{staticintcount=0;staticvoidMain(){ThreadStartjob=newThreadStart(ThreadJob);Threadthread=newThread(job);thread.Start();for(inti=0;i<5;i++){inttmp=count;Console.WriteLine("Readcount={0}",tmp);Thread.Sleep(50);tmp++;Console.WriteLine("Incrementedtmpto{0}",tmp);Thread.Sleep(20);count=tmp;Console.WriteLine("Writtencount={0}",tmp);Thread.Sleep(30);}thread.Join();Console.WriteLine("Finalcount:{0}",count);}staticvoidThreadJob(){for(inti=0;i<5;i++){inttmp=count;Console.WriteLine(" Readcount={0}",tmp);Thread.Sleep(20);tmp++;Console.WriteLine(" Incrementedtmpto{0}",tmp);Thread.Sleep(10);count=tmp;Console.WriteLine(" Writtencount={0}",tmp);Thread.Sleep(40);}}}[code]Readcount=0Readcount=0Incrementedtmpto1Writtencount=1Incrementedtmpto1Writtencount=1Readcount=1Incrementedtmpto2Readcount=1Writtencount=2Readcount=2Incrementedtmpto2Incrementedtmpto3Writtencount=2Writtencount=3Readcount=3Readcount=3Incrementedtmpto4Incrementedtmpto4Writtencount=4Writtencount=4Readcount=4Readcount=4Incrementedtmpto5Writtencount=5Incrementedtmpto5Writtencount=5Readcount=5Incrementedtmpto6Writtencount=6Finalcount:6
复制代码 [/code][/code]再对照上面这个例子:
//利用Monitor.Enter/Exit
//MonitorThread.cs
usingSystem;
usingSystem.Threading;
publicclassTest
{
staticintcount=0;
staticreadonlyobjectcountLock=newobject();
staticvoidMain()
{
ThreadStartjob=newThreadStart(ThreadJob);
Threadthread=newThread(job);
thread.Start();
for(inti=0;i<5;i++)
{
Monitor.Enter(countLock);
inttmp=count;
Console.WriteLine("Readcount={0}",tmp);
Thread.Sleep(50);
tmp++;
Console.WriteLine("Incrementedtmpto{0}",tmp);
Thread.Sleep(20);
count=tmp;
Console.WriteLine("Writtencount={0}",tmp);
Monitor.Exit(countLock);
Thread.Sleep(30);
}
thread.Join();
Console.WriteLine("Finalcount:{0}",count);
}
staticvoidThreadJob()
{
for(inti=0;i<5;i++)
{
Monitor.Enter(countLock);
inttmp=count;
Console.WriteLine(" Readcount={0}",tmp);
Thread.Sleep(20);
tmp++;
Console.WriteLine(" Incrementedtmpto{0}",tmp);
Thread.Sleep(10);
count=tmp;
Console.WriteLine(" Writtencount={0}",tmp);
Monitor.Exit(countLock);
Thread.Sleep(40);
}
}
}
了局与上例InnerDataThread.cs是纷歧样的,缘故原由就在于Monitor的利用了。- Readcount=0Incrementedtmpto1Writtencount=1Readcount=1Incrementedtmpto2Writtencount=2Readcount=2Incrementedtmpto3Writtencount=3Readcount=3Incrementedtmpto4Writtencount=4Readcount=4Incrementedtmpto5Writtencount=5Readcount=5Incrementedtmpto6Writtencount=6Readcount=6Incrementedtmpto7Writtencount=7Readcount=7Incrementedtmpto8Writtencount=8Readcount=8Incrementedtmpto9Writtencount=9Readcount=9Incrementedtmpto10Writtencount=10Finalcount:10
复制代码 上面利用lock来锁定线程:
//LockThread.cs
usingSystem;
usingSystem.Threading;
publicclassTest
{
staticintcount=0;
staticreadonlyobjectcountLock=newobject();
staticvoidMain()
{
ThreadStartjob=newThreadStart(ThreadJob);
Threadthread=newThread(job);
thread.Start();
for(inti=0;i<5;i++)
{
lock(countLock)
{
inttmp=count;
Console.WriteLine("Readcount={0}",tmp);
Thread.Sleep(50);
tmp++;
Console.WriteLine("Incrementedtmpto{0}",tmp);
Thread.Sleep(20);
count=tmp;
Console.WriteLine("Writtencount={0}",tmp);
}
Thread.Sleep(30);
}
thread.Join();
Console.WriteLine("Finalcount:{0}",count);
}
staticvoidThreadJob()
{
for(inti=0;i<5;i++)
{
lock(countLock)
{
inttmp=count;
Console.WriteLine(" Readcount={0}",tmp);
Thread.Sleep(20);
tmp++;
Console.WriteLine(" Incrementedtmpto{0}",tmp);
if(count<100)
thrownewException();
Thread.Sleep(10);
count=tmp;
Console.WriteLine(" Writtencount={0}",tmp);
}
Thread.Sleep(40);
}
}
}
了局怎样?与MonitorThread.cs对照一下,再想一想看。
因为各系统的API不同,代码调用API编写程序就会遇到很多不兼容的地方,比如Java改写后的Serv-U就不能在手机上执行,手机的游戏也不能直接在微机上执行。 |
|