1 //record when the processing started2Stopwatch Paddingtime =NewStopwatch ();3 Paddingtime.start ();4Jobhelper Jobhelper =NewJobhelper ();5 intOrderCount =Jobhelper.getjobcount ();6mutex=NewMutex (false,"Cnblogs.emulationsystem");7 if(!mutex. WaitOne (0,false))8 {9Console.Out.WriteLine ("Monitor already running ...");Ten return; One } A //TaskList (OrderCount, jobhelper); - intMaxpronumber = -; - using(_semaphorelimit=NewSemaphore (0, Maxpronumber)) the { - //exits the semaphore at a specified number of times and returns the previous count - _semaphorelimit.release (maxpronumber); - varTaskList =NewList<task>(); + for(inti =0; i < OrderCount; i++) - { + inttemp =i; A //if Submitjob has IO or other exceptions that can easily be caused by a conflict, you need to add lock at //Lock (Lockerobj) - //{ - _semaphorelimit.waitone (); -Tasklist.add (Task.Factory.StartNew () = - { - jobhelper.submitjob (temp); in //exits the semaphore and returns the previous count - if(_semaphorelimit! =NULL) _semaphorelimit.release (); to })); + //} - } the Task.waitall (Tasklist.toarray ()); * } $ paddingtime.stop ();Panax Notoginseng varTime =paddingtime.elapsedmilliseconds; -Console.WriteLine (string. Format ("Complete : {0}, cost {1} ms", Ordercount,time));
1 Public classJobhelper2 {3 Public intGetjobcount ()4 {5 return -;6 }7 8 Public BOOLSubmitJob (intjobId)9 {TenThread.Sleep ( +); One return true; A } -}
Processing tasks with multi-core processors