1. 自定义QuartZ的并发锁
 1     using System;  
 2     using System.Threading;  
 3     using log4net;  
 4     using Quartz;  
 5       
 6     namespace HK.Gramma.Business  
 7     {  
 8         public class FirstTimeJob : IJob  
 9         {  
10             /// <summary>  
11             ///     日志工具  
12             /// </summary>  
13             private static readonly ILog Logger = LogManager.GetLogger("FirstTimeJob");  
14       
15             /// <summary>  
16             ///     线程对象  
17             /// </summary>  
18             private static readonly object SyncObj = new object();  
19       
20             /// <summary>  
21             ///     任务运行标识  
22             /// </summary>  
23             private static bool _isRunning;  
24              
25             /// <summary>  
26             ///     线程访问计数  
27             /// </summary>  
28            private static int _visitCount = 0;  
29       
30             /// <summary>  
31             ///     执行定时任务  
32             /// </summary>  
33             /// <param name="context">上下文</param>  
34             public void Execute(IJobExecutionContext context)  
35             {  
36                 lock (SyncObj)  
37                 {  
38                     if (_isRunning)  
39                     {  
40                         return;  
41                     }  
42       
43                     _isRunning = true;  
44                 }  
45       
46                 try  
47                 {  
48                     Logger.InfoFormat("开始任务,线程ID={0}", Thread.CurrentThread.ManagedThreadId);  
49                       Thread.Sleep(5000);  
50                     _visitCount++;  
51                     Logger.InfoFormat("结束任务,线程ID={0},访问总数={1}", Thread.CurrentThread.ManagedThreadId, _visitCount);            }  
52                 catch (Exception ex)  
53                 {  
54                     Logger.ErrorFormat("任务异常\r\n{0}", ex);  
55                 }  
56                 finally  
57                 {  
58                     lock (SyncObj)  
59                     {  
60                         _isRunning = false;  
61                     }  
62                 }  
63             }  
64         }  
65     }  
View Code

 2. 使用QuartZ. DisallowConcurrentExecutionAttribute 可以简化并发锁的处理。

 1     using System;  
 2     using System.Threading;  
 3     using log4net;  
 4     using Quartz;  
 5       
 6     namespace HK.Gramma.Business  
 7     {  
 8         [DisallowConcurrentExecution]  
 9         public class SecondTimeJob : IJob  
10     {  
11             /// <summary>  
12             ///     线程访问计数  
13             /// </summary>  
14             private static int _visitCount = 0;  
15       
16             /// <summary>  
17             ///     日志工具  
18             /// </summary>  
19             private static readonly ILog Logger = LogManager.GetLogger("SecondTimeJob");  
20       
21             /// <summary>  
22             ///     执行定时任务  
23             /// </summary>  
24             /// <param name="context">上下文</param>  
25             public void Execute(IJobExecutionContext context)  
26             {  
27                 try  
28                 {  
29                     Logger.InfoFormat("开始任务,线程ID={0}", Thread.CurrentThread.ManagedThreadId);  
30                     Thread.Sleep(5000);  
31                     _visitCount++;  
32                     Logger.InfoFormat("结束任务,线程ID={0},访问总数={1}", Thread.CurrentThread.ManagedThreadId, _visitCount);  
33                 }  
34                 catch (Exception ex)  
35                 {  
36                     Logger.ErrorFormat("任务异常\r\n{0}", ex);  
37                 }  
38             }  
39         }  
40     }  
View Code

测试结果如下:

2015-09-08 20:42:30,001 [ServiceScheduler_Worker-2] INFO  SecondTimeJob [(null)] - 开始任务,线程ID=4  
2015-09-08 20:42:35,001 [ServiceScheduler_Worker-2] INFO  SecondTimeJob [(null)] - 结束任务,线程ID=4,访问总数=1  
2015-09-08 20:42:35,001 [ServiceScheduler_Worker-6] INFO  SecondTimeJob [(null)] - 开始任务,线程ID=8  
2015-09-08 20:42:40,002 [ServiceScheduler_Worker-6] INFO  SecondTimeJob [(null)] - 结束任务,线程ID=8,访问总数=2  
2015-09-08 20:42:40,002 [ServiceScheduler_Worker-9] INFO  SecondTimeJob [(null)] - 开始任务,线程ID=11  
2015-09-08 20:42:45,003 [ServiceScheduler_Worker-9] INFO  SecondTimeJob [(null)] - 结束任务,线程ID=11,访问总数=3  
2015-09-08 20:42:45,003 [ServiceScheduler_Worker-4] INFO  SecondTimeJob [(null)] - 开始任务,线程ID=6  
2015-09-08 20:42:50,004 [ServiceScheduler_Worker-4] INFO  SecondTimeJob [(null)] - 结束任务,线程ID=6,访问总数=4  
2015-09-08 20:42:50,004 [ServiceScheduler_Worker-7] INFO  SecondTimeJob [(null)] - 开始任务,线程ID=9  
2015-09-08 20:42:55,004 [ServiceScheduler_Worker-7] INFO  SecondTimeJob [(null)] - 结束任务,线程ID=9,访问总数=5  
2015-09-08 20:42:55,005 [ServiceScheduler_Worker-1] INFO  SecondTimeJob [(null)] - 开始任务,线程ID=3  
2015-09-08 20:43:00,006 [ServiceScheduler_Worker-1] INFO  SecondTimeJob [(null)] - 结束任务,线程ID=3,访问总数=6  
2015-09-08 20:43:00,007 [ServiceScheduler_Worker-5] INFO  SecondTimeJob [(null)] - 开始任务,线程ID=7  
2015-09-08 20:43:05,007 [ServiceScheduler_Worker-5] INFO  SecondTimeJob [(null)] - 结束任务,线程ID=7,访问总数=7  
2015-09-08 20:43:05,007 [ServiceScheduler_Worker-6] INFO  SecondTimeJob [(null)] - 开始任务,线程ID=8  
2015-09-08 20:43:10,008 [ServiceScheduler_Worker-6] INFO  SecondTimeJob [(null)] - 结束任务,线程ID=8,访问总数=8  
2015-09-08 20:43:10,012 [ServiceScheduler_Worker-3] INFO  SecondTimeJob [(null)] - 开始任务,线程ID=5  
2015-09-08 20:43:15,012 [ServiceScheduler_Worker-3] INFO  SecondTimeJob [(null)] - 结束任务,线程ID=5,访问总数=9  
2015-09-08 20:43:15,012 [ServiceScheduler_Worker-4] INFO  SecondTimeJob [(null)] - 开始任务,线程ID=6  
2015-09-08 20:43:20,013 [ServiceScheduler_Worker-4] INFO  SecondTimeJob [(null)] - 结束任务,线程ID=6,访问总数=10 

 posted on 2016-06-15 16:34  heoo  阅读(1066)  评论(0编辑  收藏  举报