您的位置:首页 > 其它

线程的基本操作

2016-03-01 21:17 302 查看
namespace 线程的操作演示
{
class Program
{
static void Main(string[] args)
{
//1.通过启动一个新线程执行一个无参数,无返回值的方法
Thread t = new Thread(new ThreadStart(M1));
t.IsBackground = true;
t.Start();

//2.通过线程执行一个带一个参数的方法
Thread t = new Thread(new ParameterizedThreadStart(M2));
t.IsBackground = true;
t.Start("hello");

//3.启动线程,执行带多个参数的方法
Person p = new Person();
p.Name = "小李";
p.Age = 17;
p.Gender = "女";

Thread t = new Thread(new ThreadStart(p.Say));
t.IsBackground = true;
t.Start();

//4.获取线程编号
Console.WriteLine("主线程的编号:" + Thread.CurrentThread.ManagedThreadId);

//启动一个新线程
Thread t = new Thread(new ThreadStart(() =>
{
Console.WriteLine("这是一个新线程,线程编号是:" + Thread.CurrentThread.ManagedThreadId);
}));
t.IsBackground = true;
t.Start();

Console.WriteLine("ok");
Console.Read();
}

static void M2(object obj)
{
Console.WriteLine(obj);
}

static void M1()
{
for (int i = 0; i < 100; i++)
{
Console.WriteLine(". ");
}
}
}

public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Gender { get; set; }

public void Say()
{
Console.WriteLine(this.Name + "   " + this.Age + "   " + this.Gender);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: