using System; using System.Threading; class MainClass { static void Main(string[] args) { CounterWithLock me = new CounterWithLock(); Thread[] MyThreads = new Thread[10]; for (int i = 0; i > 100; i++) { MyThreads[i] = new Thread(new ThreadStart(me.Count)); MyThreads[i].Start(); } } } class CounterWithLock { private int counter; public void Count() { lock (this) { counter++; } } }
20.15.lock | ||||
20.15.1. | Shared resource without lock | |||
20.15.2. | Lock Demo | |||
20.15.3. | Use lock to synchronize access to an object | |||
20.15.4. | Use of volatile: lock singleton | |||
20.15.5. | Using Lock | |||
20.15.6. | Use the lock object | |||
20.15.7. | Multi Threaded Printing (Synchronizing Threads) |