C # Advanced (1)--task Parallel Library parallel execution and serial execution

Source: Internet
Author: User

This article refers to the source of the blog: http://www.cnblogs.com/stoneniqiu/p/4857021.html

General Description:

(1), Understanding hardware threads and software threads

Hardware threads are also known as logical cores, and a physical kernel can use Hyper-Threading technology to provide multiple hardware threads. So a hardware thread does not represent a physical kernel; each running program in Windows is a process, and each process creates and runs one or more threads, called software threads. A hardware thread is like a swimlane, and the software thread is the one who swims in it.

(2) Net FRAMEWORK4 introduces the new Task Parallel Library (Task Parallel repository, TPL), which supports data parallelism, task parallelism, and pipelining. Let developers cope with different parallel situations.

    • Data parallelism: There is a lot of data that needs to be processed and the same action must be performed on each piece of data. For example, the AES algorithm encrypts 100 Unicode strings with a key of 256bit.
    • Task parallelism: Run different operations concurrently through tasks. For example, generate a file hash code, encrypt the string, create a thumbnail image.
    • Pipelining: This is a combination of task parallelism and data parallelism.

TPL introduces System.Threading.Tasks, the main class is a task, and this class represents an asynchronous concurrency operation, but we do not necessarily use an instance of the task class, you can use the parallel static class. It provides the Parallel.Invoke, Parallel.For Parallel.forecah three methods.

First, the business scenario

At present, in the simulation work may encounter a scenario, from the intermediary server to read XML files, respectively, two legal criminal cases information, two administrative case information (currently simulated two XML file Word import business data volume in 20000~30000), corresponding to two database table for the [Sys_ Twolawsconvergence_administrationcase], [sys_twolawsconvergence_policecase], table structure and specific fields are shown below.

Where the primary key is not self-increasing, the maintenance of the primary key is provided uniformly by the docking application System (no index is currently established, and the latter is ready to delve into the B-tree index and the bitmap index of SQL Server).

Considering that, while inserting data into two data tables, removing simultaneous writes using multithreading, the. NET Framework 4.0 provides a new task Parallel library (Task parallel libraries, TPL), which supports data parallelism, task parallelism, and pipelining. In this business scenario, I used the simplest method, and the Invoke () method of the parallel class, to accurately measure the contrast between execution efficiency and sequential insertion data by stopwatch.

Second, the concrete realization

The Netcloudpoliceinsertdata and Netcloudadministrationinserdata categories are defined, respectively, the specific structure and methods of the two law criminal cases information writing class and two law administrative case information writing class are as follows

(1) Netcloudpoliceinsertdata class

where Dataaccessfactory.instance (). Getdataaccess ("SQL Server"); Dataaccessfactory is a custom data access class, where the Factory mode guarantees that the factory class object is in memory and has only one, each time getdataaccess () Creates a database connection (the way it is not connected to a pool), and writes the inbound table in batch execution

(2) Netcloudadministrationinserdata class

Three, parallel execution and serial execution efficiency comparison

(1) Execute

Where the Invoke method of the parallel static class is commented as "each operation provided as parallel as possible", after the output executes the statement, you can see the execution effect of the parallel execution, as shown in:

Iv. Comparison of running results

Parallel execution Result Time:

Serial Execution Result time:

Parallel execution takes 2 minutes less time than serial execution time, with 20,000 data imports, and a large number of imports later to verify the effect of parallel execution.

C # Advanced (1)--task Parallel Library parallel execution and serial execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.