Esempio n. 1
0
        static void Main()
        {
            object obj = new object();

            if (Monitor.TryEnter(obj, 500))
            {
                try
                {
                    // acquired the lock
                    // synchronized region for obj
                }
                finally
                {
                    Monitor.Exit(obj);
                }
            }
            else
            {
                // didn't get the lock, do something else
            }

            int         numThreads = 20;
            SharedState state      = new SharedState();

            Thread[] threads = new Thread[numThreads];

            for (int i = 0; i < numThreads; i++)
            {
                threads[i] = new Thread(new Task(state).DoTheTask);
                threads[i].Start();
            }

            //for (int i = 0; i < numThreads; i++)
            //{
            //   threads[i].Join();
            //}


            Console.WriteLine("summarized {0}", state.State);
        }
Esempio n. 2
0
 public Task(SharedState sharedState)
 {
     this.sharedState = sharedState;
 }