class Program
{
static int LogCount = 100;
static int WritedCount = 0;
static int FailedCount = 0;
static void Main(string[] args)
{
//迭代运行写入日志记录,由于多个线程同时写入同一个文件将会导致错误
Parallel.For(0, LogCount, e =>
{
WriteLog();
});
Console.WriteLine(string.Format("\r\nLog Count:{0}.\t\tWrited Count:{1}.\tFailed Count:{2}.", LogCount.ToString(), WritedCount.ToString(), FailedCount.ToString()));
Console.Read();
}
static void WriteLog()
{
try
{
var logFilePath = "log.txt";
var now = DateTime.Now;
var logContent = string.Format("Tid: {0}{1} {2}.{3}\r\n", Thread.CurrentThread.ManagedThreadId.ToString().PadRight(4), now.ToLongDateString(), now.ToLongTimeString(), now.Millisecond.ToString());
File.AppendAllText(logFilePath, logContent);
WritedCount++;
}
catch (Exception ex)
{
FailedCount++;
Console.WriteLine(ex.Message);
}
}
}
class Program
{
static int LogCount = 100;
static int WritedCount = 0;
static int FailedCount = 0;
static void Main(string[] args)
{
//迭代运行写入日志记录
Parallel.For(0, LogCount, e =>
{
WriteLog();
});
Console.WriteLine(string.Format("\r\nLog Count:{0}.\t\tWrited Count:{1}.\tFailed Count:{2}.", LogCount.ToString(), WritedCount.ToString(), FailedCount.ToString()));
Console.Read();
}
//读写锁,当资源处于写入模式时,其他线程写入需要等待本次写入结束之后才能继续写入
static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim();
static void WriteLog()
{
try
{
//设置读写锁为写入模式独占资源,其他写入请求需要等待本次写入结束之后才能继续写入
//注意:长时间持有读线程锁或写线程锁会使其他线程发生饥饿 (starve)。 为了得到最好的性能,需要考虑重新构造应用程序以将写访问的持续时间减少到最小。
// 从性能方面考虑,请求进入写入模式应该紧跟文件操作之前,在此处进入写入模式仅是为了降低代码复杂度
// 因进入与退出写入模式应在同一个try finally语句块内,所以在请求进入写入模式之前不能触发异常,否则释放次数大于请求次数将会触发异常
LogWriteLock.EnterWriteLock();
var logFilePath = "log.txt";
var now = DateTime.Now;
var logContent = string.Format("Tid: {0}{1} {2}.{3}\r\n", Thread.CurrentThread.ManagedThreadId.ToString().PadRight(4), now.ToLongDateString(), now.ToLongTimeString(), now.Millisecond.ToString());
File.AppendAllText(logFilePath, logContent);
WritedCount++;
}
catch (Exception)
{
FailedCount++;
}
finally
{
//退出写入模式,释放资源占用
//注意:一次请求对应一次释放
// 若释放次数大于请求次数将会触发异常[写入锁定未经保持即被释放]
// 若请求处理完成后未释放将会触发异常[此模式不下允许以递归方式获取写入锁定]
LogWriteLock.ExitWriteLock();
}
}
}
class Program
{
static int LogCount = 1000;
static int SumLogCount = 0;
static int WritedCount = 0;
static int FailedCount = 0;
static void Main(string[] args)
{
//往线程池里添加一个任务,迭代写入N个日志
SumLogCount += LogCount;
ThreadPool.QueueUserWorkItem((obj) =>
{
Parallel.For(0, LogCount, e =>
{
WriteLog();
});
});
//在新的线程里,添加N个写入日志的任务到线程池
SumLogCount += LogCount;
var thread1 = new Thread(() =>
{
Parallel.For(0, LogCount, e =>
{
ThreadPool.QueueUserWorkItem((subObj) =>
{
WriteLog();
});
});
});
thread1.IsBackground = false;
thread1.Start();
//添加N个写入日志的任务到线程池
SumLogCount += LogCount;
Parallel.For(0, LogCount, e =>
{
ThreadPool.QueueUserWorkItem((obj) =>
{
WriteLog();
});
});
//在新的线程里,迭代写入N个日志
SumLogCount += LogCount;
var thread2 = new Thread(() =>
{
Parallel.For(0, LogCount, e =>
{
WriteLog();
});
});
thread2.IsBackground = false;
thread2.Start();
//在当前线程里,迭代写入N个日志
SumLogCount += LogCount;
Parallel.For(0, LogCount, e =>
{
WriteLog();
});
Console.WriteLine("Main Thread Processed.\r\n");
while (true)
{
Console.WriteLine(string.Format("Sum Log Count:{0}.\t\tWrited Count:{1}.\tFailed Count:{2}.", SumLogCount.ToString(), WritedCount.ToString(), FailedCount.ToString()));
Console.ReadLine();
}
}
//读写锁,当资源处于写入模式时,其他线程写入需要等待本次写入结束之后才能继续写入
static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim();
static void WriteLog()
{
try
{
//设置读写锁为写入模式独占资源,其他写入请求需要等待本次写入结束之后才能继续写入
//注意:长时间持有读线程锁或写线程锁会使其他线程发生饥饿 (starve)。 为了得到最好的性能,需要考虑重新构造应用程序以将写访问的持续时间减少到最小。
// 从性能方面考虑,请求进入写入模式应该紧跟文件操作之前,在此处进入写入模式仅是为了降低代码复杂度
// 因进入与退出写入模式应在同一个try finally语句块内,所以在请求进入写入模式之前不能触发异常,否则释放次数大于请求次数将会触发异常
LogWriteLock.EnterWriteLock();
var logFilePath = "log.txt";
var now = DateTime.Now;
var logContent = string.Format("Tid: {0}{1} {2}.{3}\r\n", Thread.CurrentThread.ManagedThreadId.ToString().PadRight(4), now.ToLongDateString(), now.ToLongTimeString(), now.Millisecond.ToString());
File.AppendAllText(logFilePath, logContent);
WritedCount++;
}
catch (Exception)
{
FailedCount++;
}
finally
{
//退出写入模式,释放资源占用
//注意:一次请求对应一次释放
// 若释放次数大于请求次数将会触发异常[写入锁定未经保持即被释放]
// 若请求处理完成后未释放将会触发异常[此模式不下允许以递归方式获取写入锁定]
LogWriteLock.ExitWriteLock();
}
}
}
... Tid: 36 2016年12月11日 15:29:22.825 Tid: 29 2016年12月11日 15:29:22.830 Tid: 6 2016年12月11日 15:29:22.838 Tid: 26 2016年12月11日 15:29:22.845 Tid: 34 2016年12月11日 15:29:22.854 Tid: 24 2016年12月11日 15:29:22.863 Tid: 27 2016年12月11日 15:29:22.872 Tid: 14 2016年12月11日 15:29:22.877 Tid: 23 2016年12月11日 15:29:22.886 Tid: 20 2016年12月11日 15:29:22.892 Tid: 30 2016年12月11日 15:29:22.898 Tid: 9 2016年12月11日 15:29:22.904 Tid: 21 2016年12月11日 15:29:22.909 Tid: 22 2016年12月11日 15:29:22.915 Tid: 7 2016年12月11日 15:29:22.920 Tid: 3 2016年12月11日 15:29:22.925 Tid: 12 2016年12月11日 15:29:22.931 Tid: 5 2016年12月11日 15:29:22.937 Tid: 13 2016年12月11日 15:29:22.942 Tid: 11 2016年12月11日 15:29:22.947 Tid: 19 2016年12月11日 15:29:22.953 Tid: 37 2016年12月11日 15:29:22.958 Tid: 37 2016年12月11日 15:29:22.964 Tid: 40 2016年12月11日 15:29:22.970 Tid: 40 2016年12月11日 15:29:22.975 Tid: 40 2016年12月11日 15:29:22.980 Tid: 40 2016年12月11日 15:29:22.985 Tid: 40 2016年12月11日 15:29:22.991 Tid: 40 2016年12月11日 15:29:22.997 Tid: 31 2016年12月11日 15:29:23.3 Tid: 31 2016年12月11日 15:29:23.9 Tid: 31 2016年12月11日 15:29:23.14 Tid: 31 2016年12月11日 15:29:23.20 Tid: 31 2016年12月11日 15:29:23.27 Tid: 31 2016年12月11日 15:29:23.33 Tid: 31 2016年12月11日 15:29:23.38 Tid: 31 2016年12月11日 15:29:23.44 Tid: 31 2016年12月11日 15:29:23.49 Tid: 31 2016年12月11日 15:29:23.57 Tid: 31 2016年12月11日 15:29:23.63 Tid: 31 2016年12月11日 15:29:23.68 Tid: 31 2016年12月11日 15:29:23.74 Tid: 16 2016年12月11日 15:29:23.80 Tid: 16 2016年12月11日 15:29:23.86 Tid: 16 2016年12月11日 15:29:23.93 Tid: 16 2016年12月11日 15:29:23.99 Tid: 16 2016年12月11日 15:29:23.105 Tid: 16 2016年12月11日 15:29:23.110 Tid: 16 2016年12月11日 15:29:23.116 Tid: 38 2016年12月11日 15:29:23.122 Tid: 38 2016年12月11日 15:29:23.128 Tid: 28 2016年12月11日 15:29:23.134 Tid: 19 2016年12月11日 15:29:23.139 Tid: 25 2016年12月11日 15:29:23.146 Tid: 37 2016年12月11日 15:29:23.152 Tid: 39 2016年12月11日 15:29:23.158 Tid: 32 2016年12月11日 15:29:23.164 Tid: 33 2016年12月11日 15:29:23.170 Tid: 31 2016年12月11日 15:29:23.176 Tid: 35 2016年12月11日 15:29:23.182 Tid: 40 2016年12月11日 15:29:23.189 Tid: 15 2016年12月11日 15:29:23.194 Tid: 18 2016年12月11日 15:29:23.202 Tid: 17 2016年12月11日 15:29:23.208 Tid: 10 2016年12月11日 15:29:23.215 Tid: 16 2016年12月11日 15:29:23.221
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有