Best practice for multithreading design

Consider this problem: I have a program that should display (say) 100 records from a database, and then for each of them it should receive updated information from a web service. In this scenario, you can introduce parallelism in two ways:

  • I run every request to the web service on a new topic. The number of simultaneous threads is controlled by some external parameter (or dynamically configured in some way).

  • I create smaller batches (let alone 10 records), and start each batch in a separate thread (therefore, taking our example, 10 threads).

Which approach is better, and why do you think so?

+3
source share
4 answers

Option 3 is the best:

Use Async IO.

, 99% HTTP-.

, Async IO. Windows ( .net framework - ) "" .

, .NET . , Win32 api. (!), # 3:

using System.Net; // need this somewhere

// need to declare an class so we can cast our state object back out
class RequestState {
    public WebRequest Request { get; set; }
}

static void Main( string[] args ) {
    // stupid cast neccessary to create the request
    HttpWebRequest request = WebRequest.Create( "http://www.stackoverflow.com" ) as HttpWebRequest;

    request.BeginGetResponse(
        /* callback to be invoked when finished */
        (asyncResult) => { 
            // fetch the request object out of the AsyncState
            var state = (RequestState)asyncResult.AsyncState; 
            var webResponse = state.Request.EndGetResponse( asyncResult ) as HttpWebResponse;

            // there we go;
            Debug.Assert( webResponse.StatusCode == HttpStatusCode.OK ); 

            Console.WriteLine( "Got Response from server:" + webResponse.Server );
        },
        /* pass the request through to our callback */
        new RequestState { Request = request }  
    );

    // blah
    Console.WriteLine( "Waiting for response. Press a key to quit" );
    Console.ReadKey();
}

EDIT:

.NET " " ThreadPool, , - , .

+6

, .

1. ?

, . , .

, , (1 ), , .

2. ?

threadpool, , , . , .

+3

, , , , : , HTTP- keep-alive, GET , TCP/IP. , , .net. ( .)

, . , .

0

Get Parallel Fx . Take a look at BlockingCollection. Use a stream to feed its batches of records and from 1 to n streams that pull records from the collection for maintenance. You can control the speed at which the collection is served and the number of threads that invoke web services. Make it customizable with ConfigSection and share it by loading Action Collection delegates, and you will have a nice little dispenser that you can reuse for your hearty content.

0
source

Source: https://habr.com/ru/post/1696412/


All Articles