Sunday 15 June 2014

c# - Consuming blocking collection with multiple tasks/consumers -


I have the following code that I try out to users from a source, for example, what I have to do Want to consume the blockage compilation with many consumers.

What's the right way to do this? Apart from this, what would be the number of best threads? Okay, will it depend on hardware, memory etc. or how can I do this better?

In addition to this, below the implementation, it has to be ensured that until it is empty, will I process everything in the collection?

  class program {public static readable blocking collection & lt; Users & gt; User = new blocking code & lt; Users & gt; (); Fixed zero main (string [] args) {for (int i = 0; i & lt; 100000; i ++) {var u = new user {id = i, name = "user" + i}; Users.Add (U); } Run (); } Run Vertical Zero () (for (int i = 0; i & lt; i ++) {Task Factor.StartNU (Process, Functioning Options. Langaran);}} Fixed Zero Process () {foreach ( Users in the User.GetConsumingOthermable ()) {Console.WriteLine (user.Id);}}} Public class user {public id id {get; set;} public string name {get; set;}}    

some small things

  1. you are never asked That your consumer foreach loops will never be full and forever hang out by after the initial loop for the user users.CompleteAdding () by deciding that
  2. You do not ever wait for work to finish, play () will speed up your 100 threads (which is potentially very high unless you have uncontrolled resources in your original process Do not include too much waiting for) because the car There are no foreground threads because when your main exits, they will not keep your program open
  3. You do not start your users until your producer Do not complete this work, you should turn the productive into a different thread or start it. Consumers are ready to work first when you populate the creator on the main thread.

    There is an updated version of the code with fixes

      class program {private const int maxThreads = 100; For this example, // path high private static readonly countdown event cde = new countdown event (maxtrades); Public static read-only blocking collection & lt; Users & gt; User = new blocking code & lt; Users & gt; (); Static zero main (string [] args) {run (); For (Int i = 0; i & lt; 100000; i ++) {var u = new user {id = i, name = "user" + i}; Users.Add (U); } Users.CompleteAdding (); Cde.Wait (); } Run static void () (for (int i = 0; i & lt; maxtreads; i ++) {Task factor.startu (process, operation option. Langarring);}} Fixed zero process () {foreach () Users in the User.GetConSummingOtherMerable ()) {Console.WriteLine (user.Id);} Cde.Signal ();}} Public Class User {Public Ent ID (get; set;} public string name {get} set;}}   

    As I said earlier, the "best thread" A, it really depends on what you are seeing.

    If you are processing then the CPU is bound, the probability of the optimum number of threads

    if you are It is waiting on external resources, but new requests do not affect the old requests (for example 20 different server information, load on the server n does not affect the load on the server In that case, I have for you

    If you are waiting on a resource (for example reading / writing hard disk) you will not want to use too many threads (maybe using only one Do) I just post about it, when reading from the hard disk, you should only use one thread at a time so that the hard drive is doing its best to read it all at once. >

No comments:

Post a Comment