线程的举例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Threading;
namespace threading
{
public class alpha
{
public void beta()
{
while (true)
Console.WriteLine("alpha.bata is running in its own thread.");
}
}
class program
{
static void Main()
{
Console.WriteLine("thread start/stop/join sample");
alpha oalpha = new alpha();
Thread othread = new Thread(new ThreadStart(oalpha.beta));
othread.Start();
while (!othread.IsAlive) ; //线程是否被激活,直到激活之后,程序才往下执行
Thread.Sleep(1); //线程休眠
othread.Abort(); //终止线程
othread.Join(); //阻止掉哟线程,直到该线程终止或经过了指定时间为止
Console.WriteLine();
Console.WriteLine("alpha.bata has finished");
try
{
Console.WriteLine("try to restart thr alpha.bata thread");
othread.Start(); //用abort()终止的线程是不可恢复的线程
}
catch (ThreadStateException) //异常处理
{
Console.WriteLine("threadstartexception trying to restart alpha.bata.");
Console.WriteLine("expectes since aborted threads cannot e restarted.");
Console.Read();
}
}
}
}
多线程同步:进程中同时存在几个执行体,接几条不同的执行线索共同工作的情况,也就是在一个进程中可以同时运行多个不同的线程来执行不同的任务
1.加锁实现同步
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Threading;
namespace locktest
{
public class dataclass
{
public static int data1 = 0;
public static int data2 = 0;
}
public class checkclass
{
public void check()
{
while (true)
{
lock (typeof(dataclass)) //锁定共享数据
{
if (dataclass.data1 != dataclass.data2)
Console.WriteLine("线程不同步");
}
}
}
}
public class setdata1
{
public void method1()
{
while (true)
{
lock (typeof(dataclass))
{
dataclass.data1 = 0;
Console.WriteLine(dataclass.data1);
Thread.Sleep(40);
dataclass.data2 = 0;
}
}
}
}
public class setdata2
{
public void method2()
{
while (true)
{
lock (typeof(dataclass))
{
dataclass.data1 = 1;
Console.WriteLine(dataclass.data1);
Thread.Sleep(40);
dataclass.data2 = 1;
}
}
}
}
public class maintest
{
public static void Main()
{
setdata1 p1 = new setdata1();
setdata2 p2 = new setdata2();
checkclass p3 = new checkclass();
Thread th1 = new Thread(new ThreadStart(p1.method1));
Thread th2 = new Thread(new ThreadStart(p2.method2));
Thread th3 = new Thread(new ThreadStart(p3.check));
th1.Start();
th2.Start();
th3.Start();
while (true)
{
if (Console.Read() == 'E')
{
th1.Abort();
th2.Abort();
th3.Abort();
break;
}
}
}
}
}
2.监视器 monitor
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Threading;
namespace monitortest
{
class program
{
public void print1() //输出<10的奇数
{
Monitor.Enter(this); //开始进行监视
for (int i = 1; i < 10; i++)
{
Monitor.Wait(this); //一开始进入等待状态,等待其他线程调用pulse或pulseall
if (i % 2 == 1)
Console.WriteLine("oddnumber = {0}", i);
Monitor.Pulse(this); //让等待状态的线程进入就绪
}
Monitor.Exit(this); //结束监视
}
public void print2()
{
Monitor.Enter(this); //开始进行监视
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0)
Console.WriteLine("evennumber = {0}", i);
Monitor.Pulse(this); //让等待状态的线程进入就绪
Monitor.Wait(this); //进入等待状态
}
Monitor.Exit(this); //结束监视
}
}
class maintest
{
public static void Main()
{
program oddeven = new program();
Thread th1 = new Thread(new ThreadStart(oddeven.print1));
Thread th2 = new Thread(new ThreadStart(oddeven.print2));
th1.Start();
th2.Start();
Console.Read();
th1.Abort();
th2.Abort();
}
}
}