通过线程计数器和Thread.Join方法得到线程已完成状态
2015-08-11 23:24
701 查看
方法一:线程计数器
class Program
{
static void Main(string[] args)
{
Thread[] ths = new Thread[3];
ThreadCounter counter1 = new ThreadCounter(1000);
ThreadCounter counter2 = new ThreadCounter(3000);
ThreadCounter counter3 = new ThreadCounter(7000);
Thread countera = new Thread(new ThreadStart(counter1.run));
Thread counterb = new Thread(new ThreadStart(counter2.run));
Thread counterc = new Thread(new ThreadStart(counter3.run));
countera.Start();
counterb.Start();
counterc.Start();
Console.ReadKey();
}
}
class ThreadCounter
{
private static int count = 0;
private int ms;
private static void Increment()
{
lock (typeof(ThreadCounter))
{
count++;
}
}
private static void Decrease()
{
lock (typeof(ThreadCounter))
{
count--;
}
}
private static int getCount()
{
lock (typeof(ThreadCounter))
{
return count;
}
}
public ThreadCounter(int ms)
{
Increment();
this.ms = ms;
}
public void run()
{
Thread.Sleep(ms);
Console.WriteLine(ms.ToString() + "毫秒任务结束");
Decrease();
if (getCount() == 0)
{
Console.WriteLine("-------所有任务结束-------");
}
}
}
方法二:Thread.Join方法
class Program
{
static void Main(string[] args)
{
ThreadJoin join1 = new ThreadJoin(1000);
ThreadJoin join2 = new ThreadJoin(3000);
ThreadJoin join3 = new ThreadJoin(7000);
Thread th1 = new Thread(new ThreadStart(join1.run));
Thread th2 = new Thread(new ThreadStart(join2.run));
Thread th3 = new Thread(new ThreadStart(join3.run));
ThreadJoin join = new ThreadJoin();
join.Join(new Thread[] { th1, th2, th3 });
Console.ReadKey();
}
}
class ThreadJoin
{
private int ms;
public ThreadJoin()
{
}
public ThreadJoin(int ms)
{
this.ms = ms;
}
public void run()
{
Thread.Sleep(ms);
Console.WriteLine(ms + "毫秒任务结束");
}
public void Join(object[] obj)
{
Thread[] ths = obj as Thread[];
foreach (Thread th in ths)
{
th.Start();
th.Join();
}
Console.WriteLine("所有线程结束");
}
}
转载自:http://developer.51cto.com/art/200908/147794.htm 自己做了修改
推荐:http://www.laikanxia.com
class Program
{
static void Main(string[] args)
{
Thread[] ths = new Thread[3];
ThreadCounter counter1 = new ThreadCounter(1000);
ThreadCounter counter2 = new ThreadCounter(3000);
ThreadCounter counter3 = new ThreadCounter(7000);
Thread countera = new Thread(new ThreadStart(counter1.run));
Thread counterb = new Thread(new ThreadStart(counter2.run));
Thread counterc = new Thread(new ThreadStart(counter3.run));
countera.Start();
counterb.Start();
counterc.Start();
Console.ReadKey();
}
}
class ThreadCounter
{
private static int count = 0;
private int ms;
private static void Increment()
{
lock (typeof(ThreadCounter))
{
count++;
}
}
private static void Decrease()
{
lock (typeof(ThreadCounter))
{
count--;
}
}
private static int getCount()
{
lock (typeof(ThreadCounter))
{
return count;
}
}
public ThreadCounter(int ms)
{
Increment();
this.ms = ms;
}
public void run()
{
Thread.Sleep(ms);
Console.WriteLine(ms.ToString() + "毫秒任务结束");
Decrease();
if (getCount() == 0)
{
Console.WriteLine("-------所有任务结束-------");
}
}
}
方法二:Thread.Join方法
class Program
{
static void Main(string[] args)
{
ThreadJoin join1 = new ThreadJoin(1000);
ThreadJoin join2 = new ThreadJoin(3000);
ThreadJoin join3 = new ThreadJoin(7000);
Thread th1 = new Thread(new ThreadStart(join1.run));
Thread th2 = new Thread(new ThreadStart(join2.run));
Thread th3 = new Thread(new ThreadStart(join3.run));
ThreadJoin join = new ThreadJoin();
join.Join(new Thread[] { th1, th2, th3 });
Console.ReadKey();
}
}
class ThreadJoin
{
private int ms;
public ThreadJoin()
{
}
public ThreadJoin(int ms)
{
this.ms = ms;
}
public void run()
{
Thread.Sleep(ms);
Console.WriteLine(ms + "毫秒任务结束");
}
public void Join(object[] obj)
{
Thread[] ths = obj as Thread[];
foreach (Thread th in ths)
{
th.Start();
th.Join();
}
Console.WriteLine("所有线程结束");
}
}
转载自:http://developer.51cto.com/art/200908/147794.htm 自己做了修改
推荐:http://www.laikanxia.com
相关文章推荐
- java-模拟tomcat服务器
- 设计模式之行为型模式 - 调用行为的传递问题
- c#调用COM组件
- C#实现把指定数据写入串口
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#通用邮件发送类分享
- C#中this的用法集锦
- C#.NET获取拨号连接的宽带连接方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#生成饼形图及添加文字说明实例代码
- C#判等对象是否相等的方法汇总
- C#简单的向量用法实例教程
- C#实现基于链表的内存记事本实例
- C#托管堆对象实例包含内容分析
- C#实现按照指定长度在数字前补0方法小结
- C# 通过 inline-asm 解决嵌入x86汇编