static void Main(string[] args)
{
Console.WriteLine("让worker函数运行5s后停止");
var t = new Thread(Worker);
t.Start();
Thread.Sleep(5000);
stop = true;
Console.ReadLine();
}
private static bool stop = false;
private static void Worker(object obj)
{
int x = 0;
while (!stop)
{
x++;
}
Console.WriteLine("worker函数停止x={0}",x);
}
class test
{
private static int m_flag = 0;
private static int m_value = 0;
public static void Thread1(object obj)
{
m_value = 5;
m_flag = 1;
}
public static void Thread2(object obj)
{
if (m_flag == 1)
Console.WriteLine("m_value = {0}", m_value);
}
//多核CPU机器才会出现线程同步问题
public void Exec()
{
var thread1 = new Thread(Thread1);
var thread2 = new Thread(Thread2);
thread1.Start();
thread2.Start();
Console.ReadLine();
}
}
//上报状态类型
enum CoordinationStatus
{
Cancel,
Timeout,
AllDone
}
class AsyncCoordinator
{
//AllBegun 内部调用JustEnded来递减它
private int _mOpCount = 1;
//0=false,1=true
private int _mStatusReported = 0;
private Action<CoordinationStatus> _mCallback;
private Timer _mTimer;
//发起一个操作之前调用
public void AboutToBegin(int opsToAdd = 1)
{
Interlocked.Add(ref _mOpCount, opsToAdd);
}
//处理好一个操作的结果之后调用
public void JustEnded()
{
if (Interlocked.Decrement(ref _mOpCount) == 0)
{
ReportStatus(CoordinationStatus.AllDone);
}
}
//该方法必须在发起所有操作后调用
public void AllBegin(Action<CoordinationStatus> callback, int timeout = Timeout.Infinite)
{
_mCallback = callback;
if (timeout != Timeout.Infinite)
{
_mTimer = new Timer(TimeExpired, null, timeout, Timeout.Infinite);
JustEnded();
}
}
private void TimeExpired(object o)
{
ReportStatus(CoordinationStatus.Timeout);
}
public void Cancel()
{
ReportStatus(CoordinationStatus.Cancel);
}
private void ReportStatus(CoordinationStatus status)
{
//如果状态从未报告过,就报告它,否则就忽略它,只调用一次
if (Interlocked.Exchange(ref _mStatusReported, 1) == 0)
{
_mCallback(status);
}
}
}
class MultiWebRequest
{
//辅助类 用于协调所有的异步操作
private AsyncCoordinator _mac = new AsyncCoordinator();
protected Dictionary<string,object> _mServers = new Dictionary<string, object>
{
{"http://www.baidu.com",null},{"http://www.Microsoft.com",null},{"http://www.cctv.com",null},
{"http://www.souhu.com",null},{"http://www.sina.com",null},{"http://www.tencent.com",null},
{"http://www.youku.com",null}
};
private Stopwatch sp;
public MultiWebRequest(int timeout = Timeout.Infinite)
{
sp = new Stopwatch();
sp.Start();
//通过异步方式一次性发起请求
var httpclient = new HttpClient();
foreach (var server in _mServers.Keys)
{
_mac.AboutToBegin(1);
httpclient.GetByteArrayAsync(server).ContinueWith(task => ComputeResult(server, task));
}
_mac.AllBegin(AllDone,timeout);
Console.WriteLine("");
}
private void ComputeResult(string server, Task<Byte[]> task)
{
object result;
if (task.Exception != null)
{
result = task.Exception.InnerException;
}
else
{
//线程池处理IO
result = task.Result.Length;
}
//保存返回结果的长度
_mServers[server] = result;
_mac.JustEnded();
}
public void Cancel()
{
_mac.Cancel();
}
private void AllDone(CoordinationStatus status)
{
sp.Stop();
Console.WriteLine("响应耗时总计{0}",sp.Elapsed);
switch (status)
{
case CoordinationStatus.Cancel:
Console.WriteLine("操作取消");
break;
case CoordinationStatus.AllDone:
Console.WriteLine("操作完成,完成的结果如下");
foreach (var server in _mServers)
{
Console.WriteLine("{0}",server.Key);
object result = server.Value;
if (result is Exception)
{
Console.WriteLine("错误原因{0}",result.GetType().Name);
}
else
{
Console.WriteLine("返回字节数为:{0}",result);
}
}
break;
case CoordinationStatus.Timeout:
Console.WriteLine("操作超时");
break;
default:
throw new ArgumentOutOfRangeException("status", status, null);
}
}
}
class SomeResource
{
private SimpleSpinLock s1 = new SimpleSpinLock();
public void AccessResource()
{
s1.Enter();
//一次是有一个线程才能进入访问
s1.Leave();
}
}
class SimpleSpinLock
{
private int _mResourceInUse;
public void Enter()
{
while (true)
{
if(Interlocked.Exchange(ref _mResourceInUse,1)==0)
return;
}
}
public void Leave()
{
Volatile.Write(ref _mResourceInUse,1);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有