c# 多线程入门demo
2016-02-16 09:52
639 查看
在上一篇博客《java—简单理解线程》中,就简单介绍在java中的多线程应用,以及通过demo来区别不用多线程和使用多线程的时间区别,通过上篇博客中的两张对比图显然能看出他们之间的区别,但是具体的方法运行时间少了多少呢?这篇博客中我们将引入,并分别用thread和task两种方式来实现多线程。
一 线程Thread的使用
(1)不通过委托,直接在线程里实现方法体
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
bool a =false;
bool b = false;
bool c = false;
Stopwatch watch = new Stopwatch();//测量运行时间
watch.Start();//开始计时
//线程1
Thread threadTest1 = new Thread(() =>
{
Thread.Sleep(2000);
Console.WriteLine("线程1结束消耗时间:{0}", watch.ElapsedMilliseconds);
a = true;//如果执行则返回true
});
//线程2
Thread threadTest2 = new Thread(() =>
{
Thread.Sleep(2000);
Console.WriteLine("线程2结束消耗时间:{0}", watch.ElapsedMilliseconds);
b = true;//如果执行则返回true
});
//线程3
Thread threadTest3 = new Thread(() =>
{
Thread.Sleep(2000);
Console.WriteLine("线程3结束消耗时间:{0}", watch.ElapsedMilliseconds);
c = true;//如果执行则返回true
});
threadTest1.Start();
threadTest2.Start();
threadTest3.Start();
threadTest2.Join();//等待其它线程执行结束
threadTest1.Join();
threadTest3.Join();
if (a == true && b == true && c == true)//当三个子线程全部执行完毕,则执行
{
Console.WriteLine("监控结束消耗时间:{0}", watch.ElapsedMilliseconds);
Console.Read();
}
}
}
} 执行结果:
![](https://img-blog.csdn.net/20160216094000482?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
(2)通过委托传递,可以带参,也可以不带参
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();//测量运行时间
watch.Start();//开始计时
//通过委托传递,将testMethod方法委托给线程执行
Thread t1 = new Thread(new ThreadStart(TestMethod));
Thread t2 = new Thread(new ParameterizedThreadStart(TestMethod));
t1.Start();
t2.Start("hello");
t1.Join();
t2.Join();
Console.WriteLine("总时间:{0}", watch.ElapsedMilliseconds);
Console.Read();
}
public static void TestMethod()
{
Console.WriteLine("不带参数的线程函数");
}
public static void TestMethod(object data)
{
string datastr = data as string;
Console.WriteLine("带参数的线程函数,参数为:{0}", datastr);
}
}
}<strong>
</strong> 执行结果:
![](https://img-blog.csdn.net/20160216094841275?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
二:使用task类实现多线程
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
bool a =false;
bool b = false;
bool c = false;
Stopwatch watch = new Stopwatch();//测量运行时间
watch.Start();//开始计时
var Task1 = Task.Factory.StartNew(() =>
{
Thread.Sleep(2000);
Console.WriteLine("线程1结束消耗时间:{0}", watch.ElapsedMilliseconds);
a = true;
});
var Task2 = Task.Factory.StartNew(() =>
{
Thread.Sleep(2000);
Console.WriteLine("线程2结束消耗时间:{0}", watch.ElapsedMilliseconds);
b = true;
});
var Task3 = Task.Factory.StartNew(() =>
{
Thread.Sleep(2000);
Console.WriteLine("线程3结束消耗时间:{0}", watch.ElapsedMilliseconds);
c = true;
});
Task.WaitAll(Task1, Task2, Task3);
if (a == true && b == true && c == true)
{
Console.WriteLine("监控结束消耗时间:{0}", watch.ElapsedMilliseconds);
Console.Read();
}
else
{
//Thread.Sleep(1);
Console.Read();
}
}
}
} 执行结果:
![](https://img-blog.csdn.net/20160216094900822?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
这三个demo只是对于刚刚接触多线程的一个实践,简单易理解。
一 线程Thread的使用
(1)不通过委托,直接在线程里实现方法体
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
bool a =false;
bool b = false;
bool c = false;
Stopwatch watch = new Stopwatch();//测量运行时间
watch.Start();//开始计时
//线程1
Thread threadTest1 = new Thread(() =>
{
Thread.Sleep(2000);
Console.WriteLine("线程1结束消耗时间:{0}", watch.ElapsedMilliseconds);
a = true;//如果执行则返回true
});
//线程2
Thread threadTest2 = new Thread(() =>
{
Thread.Sleep(2000);
Console.WriteLine("线程2结束消耗时间:{0}", watch.ElapsedMilliseconds);
b = true;//如果执行则返回true
});
//线程3
Thread threadTest3 = new Thread(() =>
{
Thread.Sleep(2000);
Console.WriteLine("线程3结束消耗时间:{0}", watch.ElapsedMilliseconds);
c = true;//如果执行则返回true
});
threadTest1.Start();
threadTest2.Start();
threadTest3.Start();
threadTest2.Join();//等待其它线程执行结束
threadTest1.Join();
threadTest3.Join();
if (a == true && b == true && c == true)//当三个子线程全部执行完毕,则执行
{
Console.WriteLine("监控结束消耗时间:{0}", watch.ElapsedMilliseconds);
Console.Read();
}
}
}
} 执行结果:
(2)通过委托传递,可以带参,也可以不带参
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();//测量运行时间
watch.Start();//开始计时
//通过委托传递,将testMethod方法委托给线程执行
Thread t1 = new Thread(new ThreadStart(TestMethod));
Thread t2 = new Thread(new ParameterizedThreadStart(TestMethod));
t1.Start();
t2.Start("hello");
t1.Join();
t2.Join();
Console.WriteLine("总时间:{0}", watch.ElapsedMilliseconds);
Console.Read();
}
public static void TestMethod()
{
Console.WriteLine("不带参数的线程函数");
}
public static void TestMethod(object data)
{
string datastr = data as string;
Console.WriteLine("带参数的线程函数,参数为:{0}", datastr);
}
}
}<strong>
</strong> 执行结果:
二:使用task类实现多线程
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
bool a =false;
bool b = false;
bool c = false;
Stopwatch watch = new Stopwatch();//测量运行时间
watch.Start();//开始计时
var Task1 = Task.Factory.StartNew(() =>
{
Thread.Sleep(2000);
Console.WriteLine("线程1结束消耗时间:{0}", watch.ElapsedMilliseconds);
a = true;
});
var Task2 = Task.Factory.StartNew(() =>
{
Thread.Sleep(2000);
Console.WriteLine("线程2结束消耗时间:{0}", watch.ElapsedMilliseconds);
b = true;
});
var Task3 = Task.Factory.StartNew(() =>
{
Thread.Sleep(2000);
Console.WriteLine("线程3结束消耗时间:{0}", watch.ElapsedMilliseconds);
c = true;
});
Task.WaitAll(Task1, Task2, Task3);
if (a == true && b == true && c == true)
{
Console.WriteLine("监控结束消耗时间:{0}", watch.ElapsedMilliseconds);
Console.Read();
}
else
{
//Thread.Sleep(1);
Console.Read();
}
}
}
} 执行结果:
这三个demo只是对于刚刚接触多线程的一个实践,简单易理解。
相关文章推荐
- c#使用win32api实现获取光标位置
- Default Access Modifiers (C#)
- c#数组总结
- C# 静态类与非静态类、静态成员的区别
- c# 数据类型
- CSharpGL(14)用geometry shader渲染模型的法线(normal)
- C#12种顺序排序
- yield
- C#_启动多个窗体winform
- C#面向对象基础
- c# 抽象类练习
- C#学习三之接口
- c# 多态
- c# 继承
- c#里,WebBrowser实现不加载图片等控制
- c#值类型与引用类型,可空类型
- 转载:C# Office 开发
- C# 图结构操作
- 从0自学C#05--窗体之间的相互访问
- C#图片添加水印的实现代码