代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Windows.Forms; 4 using System.Threading; 5 6 namespace TestThread 7 { 8 public partial class Form3 : Form 9 { 10 //死锁 11 LongLock mCnt = new LongLock(); 12 13 public Form3() 14 { 15 InitializeComponent(); 16 17 mCnt.mEventChanged += mCnt_mEventChanged; 18 } 19 20 void mCnt_mEventChanged(string arg1, long arg2, bool arg3) 21 { 22 this.Invoke(new Action( 23 () => 24 { 25 this.Text = mCnt.Value.ToString(); 26 Application.DoEvents(); 27 })); 28 } 29 30 private void Form3_Load(object sender, EventArgs e) 31 { 32 33 } 34 35 private void button1_Click(object sender, EventArgs e) 36 { 37 int i = 0; 38 while (i++ <= 20) 39 { 40 Random random = new Random(); 41 Thread t = new Thread(new ThreadStart( 42 () => 43 { 44 Thread.Sleep(random.Next(10, 20)); 45 46 mCnt.Plus(Environment.TickCount.ToString()); 47 48 })); 49 t.Start(); 50 Thread.Sleep(1); 51 }//end while 52 } 53 54 } 55 56 public class LongLock 57 { 58 object _objLock = null; 59 HashSet<string> _hashSet = null; 60 61 public event Action<string, long, bool> mEventChanged; 62 63 public LongLock() 64 { 65 _hashSet = new HashSet<string>(); 66 _objLock = new object(); 67 } 68 69 public int Value 70 { 71 get 72 { 73 lock (_objLock) 74 { 75 return _hashSet.Count; 76 } 77 } 78 } 79 80 public void Plus(string id) 81 { 82 83 lock (_objLock) 84 { 85 if (!_hashSet.Contains(id)) 86 { 87 88 _hashSet.Add(id); 89 Notice(id, _hashSet.Count, true); 90 } 91 } 92 93 } 94 95 public void Minus(string id) 96 { 97 lock (_objLock) 98 { 99 if (_hashSet.Contains(id)) 100 { 101 _hashSet.Remove(id); 102 Notice(id, _hashSet.Count, true); 103 } 104 } 105 } 106 107 void Notice(string id, long value, bool isPlus) 108 { 109 if (mEventChanged != null) 110 { 111 mEventChanged(id, value, isPlus); 112 } 113 } 114 115 116 } 117 118 119 public class LongLock2 120 { 121 object _objLock = null; 122 HashSet<string> _hashSet = null; 123 124 public event Action<string, long, bool> mEventChanged; 125 126 public LongLock2() 127 { 128 _hashSet = new HashSet<string>(); 129 _objLock = new object(); 130 } 131 132 public int Value 133 { 134 get 135 { 136 lock (_objLock) 137 { 138 return _hashSet.Count; 139 } 140 } 141 } 142 143 public void Plus(string id) 144 { 145 bool b = false; 146 long value = -1; 147 lock (_objLock) 148 { 149 if (!_hashSet.Contains(id)) 150 { 151 b = true; 152 _hashSet.Add(id); 153 value = _hashSet.Count; 154 } 155 } 156 if (b) 157 { 158 Notice(id, _hashSet.Count, true); 159 } 160 } 161 162 public void Minus(string id) 163 { 164 bool b = false; 165 long value = -1; 166 lock (_objLock) 167 { 168 if (_hashSet.Contains(id)) 169 { 170 b = true; 171 _hashSet.Remove(id); 172 value = _hashSet.Count; 173 } 174 } 175 if (b) 176 { 177 Notice(id, value, false); 178 } 179 } 180 181 void Notice(string id, long value, bool isPlus) 182 { 183 if (mEventChanged != null) 184 { 185 mEventChanged(id, value, isPlus); 186 } 187 } 188 189 190 } 191 192 193 194 }
时间: 2024-09-29 21:12:40