文件读写冲突的解决办法:ReaderWriterLock
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
HttpContext.Current = (HttpContext)context; var content = TemplateFactory.ReplaceContent(template); ReaderWriterLockHelper.WriteToResource(10, () => {
项目中碰到了静态页文件读写冲突的问题如果同时存在读写就报黄页虽然写入是瞬间的但高并发下依然有可能
文件读写冲突的解决办法: ReaderWriterLock
项目中碰到了静态页文件读写冲突的问题(如果同时存在读写就报黄页,虽然写入是“瞬间”的,但高并发下依然有可能)。
public class ReaderWriterLockHelper {
static ReaderWriterLock rwl = new ReaderWriterLock();
public static void ReadFromResource(int timeOut, Action readAction) {
try {
rwl.AcquireReaderLock(timeOut); try {
using (var sw = fileInfo.CreateText()) {
sw.WriteLine(content); sw.Close(); } }); }); thread.Start(HttpContext.Current);
读取:
ReaderWriterLockHelper.ReadFromResource(10, () => context.Response.WriteFile(url));
readAction(); } finally {
rwl.ReleaseReaderLock(); } } catch (ApplicationException) { } }
public static void WriteToResource(int timeOut, Action writeAction) {
try {
rwl.AcquireWriterLock(timeOut); try {
writeAction(); } finally {
rwl.ReleaseWriterLock(); } } catch (ApplicationException) { } } }
写入:
vቤተ መጻሕፍቲ ባይዱr thread = new Thread(context => {
项目中碰到了静态页文件读写冲突的问题如果同时存在读写就报黄页虽然写入是瞬间的但高并发下依然有可能
文件读写冲突的解决办法: ReaderWriterLock
项目中碰到了静态页文件读写冲突的问题(如果同时存在读写就报黄页,虽然写入是“瞬间”的,但高并发下依然有可能)。
public class ReaderWriterLockHelper {
static ReaderWriterLock rwl = new ReaderWriterLock();
public static void ReadFromResource(int timeOut, Action readAction) {
try {
rwl.AcquireReaderLock(timeOut); try {
using (var sw = fileInfo.CreateText()) {
sw.WriteLine(content); sw.Close(); } }); }); thread.Start(HttpContext.Current);
读取:
ReaderWriterLockHelper.ReadFromResource(10, () => context.Response.WriteFile(url));
readAction(); } finally {
rwl.ReleaseReaderLock(); } } catch (ApplicationException) { } }
public static void WriteToResource(int timeOut, Action writeAction) {
try {
rwl.AcquireWriterLock(timeOut); try {
writeAction(); } finally {
rwl.ReleaseWriterLock(); } } catch (ApplicationException) { } } }
写入:
vቤተ መጻሕፍቲ ባይዱr thread = new Thread(context => {