.NET多线程学习之龟兔赛跑
.NET多线程之龟兔赛跑
最近很浮躁,总是不能静下心来完整的看完或者听完一个Demo,话说程序员的人生就是不停的学习的过程,不学习你会被淘汰。这点我对自己十分的厌恶。真好周末时间,终于静下心来看了看多线程相关的资料,把学习结果分享给大家。
本例子的程序是一个龟兔赛跑的故事,用多线程的方法很好的解决了单线程不能实现乌龟和兔子同时跑步的情况。例子是一个控制台程序,一共包括三个类,下面把类详细代码贴出来。
第一个类:Winner类,这个类很简单,就一个静态字段,用于记录谁获得了跑步的冠军。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
本例子的程序是一个龟兔赛跑的故事,用多线程的方法很好的解决了单线程不能实现乌龟和兔子同时跑步的情况。例子是一个控制台程序,一共包括三个类,下面把类详细代码贴出来。
第一个类:Winner类,这个类很简单,就一个静态字段,用于记录谁获得了跑步的冠军。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Winner
{
public static class Winner
{
public static string winner = "";
}
}
第二个类:Runner类
namespace Runner
{
public class Runner1
{
public static class Winner
{
public static string winner = "";
}
}
第二个类:Runner类
namespace Runner
{
public class Runner1
{
//字段
#region 字段
private string _name; //跑步人的名字
public int _speed; //定义跑步的速度
#endregion
//字段
#region 字段
private string _name; //跑步人的名字
public int _speed; //定义跑步的速度
#endregion
//构造函数
public Runner1(string name, int speed)
{
this._name = name;
this._speed = speed;
}
public Runner1(string name, int speed)
{
this._name = name;
this._speed = speed;
}
//属性—Name
public string Name
{
get
{
return this._name;
}
}
public string Name
{
get
{
return this._name;
}
}
//属性-Speed
public int speed
{
get
{
return this._speed;
}
}
public int speed
{
get
{
return this._speed;
}
}
//跑步方法
public void Run(object objlength)
{
public void Run(object objlength)
{
if (objlength != null && objlength.GetType().FullName == "System.Int32")
{
Console.WriteLine("\n<" + this.Name + "听到发令枪响,开始起跑\n");
{
Console.WriteLine("\n<" + this.Name + "听到发令枪响,开始起跑\n");
int length = int.Parse(objlength.ToString());
for (int count = 0; count <= length; count += this.speed)
{
Thread.Sleep(1000);
Console.Write("\n<");
Console.Write(this.Name);
Console.Write(">");
Console.Write("跑到了第");
Console.Write("<");
Console.Write(count.ToString());
Console.Write(">");
Console.Write("米处!");
Console.Write("\n");
}
for (int count = 0; count <= length; count += this.speed)
{
Thread.Sleep(1000);
Console.Write("\n<");
Console.Write(this.Name);
Console.Write(">");
Console.Write("跑到了第");
Console.Write("<");
Console.Write(count.ToString());
Console.Write(">");
Console.Write("米处!");
Console.Write("\n");
}
Console.WriteLine("\n<" + this.Name + "跑到终点\n");
this.RecordWinner();
}
}
this.RecordWinner();
}
}
//记录谁第一个跑到终点
public void RecordWinner()
{
if (Winner.Winner.winner !="")
{
//说明this.Winner已经记录了跑步冠军
}
else
{
Winner.Winner.winner = this.Name;
}
}
}
}
第三个类:Main函数的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Runner;
using System.Threading;
using Winner;
namespace SheepRunWolf
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\n裁判打响了发令枪,嘣......\n");
//ShowsingleThreadRun();
ShowMultiThreadRun();
Console.WriteLine("\n裁判宣布:跑步比赛结束......\n");
Console.ReadLine();
}
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("\n裁判打响了发令枪,嘣......\n");
//ShowsingleThreadRun();
ShowMultiThreadRun();
Console.WriteLine("\n裁判宣布:跑步比赛结束......\n");
Console.ReadLine();
}
//单线程
public static void ShowsingleThreadRun()
{
Runner1 sheep = new Runner1("笨笨而又萌萌的小乌龟", 5);
Runner1 wolf = new Runner1("聪明伶俐又潇洒的小兔子", 6);
sheep.Run(60);
wolf.Run(60);
}
//多线程
public static void ShowMultiThreadRun()
{
Runner1 sheep = new Runner1("笨笨而又萌萌的小乌龟", 2);
Runner1 wolf = new Runner1("聪明伶俐又潇洒的小兔子", 6);
int length = 12;
Thread sheepthread = new Thread(new ParameterizedThreadStart(sheep.Run));
Thread wolfthread = new Thread(new ParameterizedThreadStart(wolf.Run));
//制作了线程,但是并还没有开始运行
Console.WriteLine("按回车后开始跑步");
Console.ReadLine();
sheepthread.Start(length );//让线程开始运行
wolfthread.Start(length);//让线程开始运行
sheepthread.Join(); //主线程等待sheepthread线程执行完成在执行
wolfthread.Join(); //主线程等待wolfthread线程执行完成在执行
Console.WriteLine("\n裁判宣布:"+ Winner.Winner.winner.ToString() + "获得了冠军\n");
}
}
}
单线程运行结果:不能实现乌龟和兔子同时进行跑步。
![图片](http://b272.photo.store.qq.com/psb?/V12P9fpR29mSFp/N.KO7QNUmwKrEkRmBwWjDeJCJrHfnZjET5iI75LjAi4!/b/dJ2uJ6I2HAAA&bo=nQLGAgAAAAABAHw!)
多线程运行结果:
![图片](http://b271.photo.store.qq.com/psb?/V12P9fpR29mSFp/zlsgvxS20iZPQwVuKQz2*dD.4DZoE8T*B1XMngumXMU!/b/dIAalaFVHwAA&bo=nQK2AgAAAAABAAw!)
}
单线程运行结果:不能实现乌龟和兔子同时进行跑步。
多线程运行结果: