The impact of using a thread pool on the number of threads produced by the application and the CPU usage time

Source: Internet
Author: User
Document directory
  • Test code
  • Running result:
  • Conclusion
Thread Pool implementation principle:

Multi-threaded unit mode, with the help of preemptive multi-task mode, using high-performance queues and scheduling.

Thread pool management refers to the set of threads created during the initialization process of multi-threaded applications. When a thread is required, the thread is reused for a new task, rather than creating a new thread. The number of threads is generally fixed. All threads in the thread pool are allocated with a task. When the task is completed, the thread returns to the thread pool and waits for the next allocation.

Advantages of thread pool:

• No need to re-create a thread

• CLR does not have to create a new thread for a short-lived task and reclaim its resources at the end.

• The thread pool optimizes time slices Based on Processes

• Start multiple threads without setting attributes for each thread

• Allows passing the state information of a thread as an object to the process parameter WaitCallback (void (object) of the current task to be executed ))

• Fixed the number of threads requested by the customer to a certain maximum value

Thread Pool restrictions:

• CLR is responsible for maintaining the thread pool. If the task has been added to the queue, the task cannot be canceled directly.

• The thread pool is very effective for short-lived tasks, but not for long-running tasks.

• The cost-efficiency ratio of the thread pool is determined based on the quantity and startup overhead. The thread pool size should be fixed

• All threads in the thread pool are in multi-threaded units. If the thread pool is placed in a single thread unit, the thread pool does not work.

• Threads in the thread pool cannot be started, suspended, or terminated manually.

• Priority cannot be specified for threads in the thread pool.

• An application can have only one thread pool

• If a task in the thread pool is locked, the thread will not be released to the pool.

Test code description:

ThreadTest description:

Start 50 threads to execute LongCalculate1; start 50 threads to execute LongCalculate2. LongCalculate1 and LongCalculate2 to execute 999 cycles respectively, and output the number of current cycles.

ThreadPoolTest description:

Add 50 methods to the thread pool: LongCalculateCallback1;

Add 50 methods to the thread pool: LongCalculateCallback2;

LongCalculateCallback1 and LongCalculateCallback2 run 999 cycles respectively and output the number of times of the current loop.

The LongCalculate1, LongCalculate2, LongCalculateCallback1, and LongCalculateCallback2 methods are executed in the same way.

Purpose:

Using performance counters to count progressThreadTest, ThreadPoolTestThe number of threads per second and the percentage of time the process thread spends executing commands using the processor.

Using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Text;

Using System. Threading;

Namespace NetThreading

{

/// <Summary>

/// Use of thread pool classes

/// </Summary>

Internal class ThreadPoolApp: IMain

{

/// <Summary>

/// Maximum number of threads

/// </Summary>

Const int maxThreadCount = 50;

/// <Summary>

/// Maximum number of cycles

/// </Summary>

Const int maxCount = 999;

/// <Summary>

/// Thread sleep time

/// </Summary>

Const int sleepTimeSpan = 0;

/// <Summary>

/// Display the thread pool information

/// </Summary>

Private void ShowThreadPoolInfo ()

{

Int workerThreads, completionPortThreads;

ThreadPool. GetMaxThreads (out workerThreads, out completionPortThreads );

Console. WriteLine ("Maximum number of auxiliary threads in the thread pool: {0}", workerThreads );

Console. WriteLine ("Maximum number of asynchronous I/O threads in the thread pool: {0}", completionPortThreads );

}

/// <Summary>

/// Use the thread pool to create multiple threads

/// </Summary>

Private void ThreadPoolTest ()

{

For (int I = 0; I <maxThreadCount; I ++)

{

ThreadPool. QueueUserWorkItem (new WaitCallback (LongCalculateCallback1 ));

ThreadPool. QueueUserWorkItem (new WaitCallback (LongCalculateCallback2 ));

}

}

/// <Summary>

/// General Multithreading

/// </Summary>

Private void ThreadTest ()

{

For (int I = 0; I <maxThreadCount; I ++)

{

Thread t1 = new Thread (new ThreadStart (LongCalculate1 ));

T1.Start ();

Thread t2 = new Thread (new ThreadStart (LongCalculate2 ));

T2.Start ();

}

}

# Region call Method

Private void LongCalculate1 ()

{

Console. WriteLine ("ThreadID: {0} LongCalculate1 Start", Thread. CurrentThread. ManagedThreadId );

For (int I = 0; I <maxCount; I ++)

{

Thread. Sleep (sleepTimeSpan );

Console. WriteLine ("ThreadID: {0} LongCalculate1 Running {1}", Thread. CurrentThread. ManagedThreadId, I );

}

Console. WriteLine ("ThreadID: {0} LongCalculate1 end", Thread. CurrentThread. ManagedThreadId );

}

Private void LongCalculate2 ()

{

Console. WriteLine ("ThreadID: {0} LongCalculate2 Start", Thread. CurrentThread. ManagedThreadId );

For (int I = 0; I <maxCount; I ++)

{

Thread. Sleep (sleepTimeSpan );

Console. WriteLine ("ThreadID: {0} LongCalculate2 Running {1}", Thread. CurrentThread. ManagedThreadId, I );

}

Console. WriteLine ("ThreadID: {0} LongCalculate2 end", Thread. CurrentThread. ManagedThreadId );

}

Private void LongCalculateCallback1 (object obj)

{

Console. WriteLine ("ThreadID: {0} LongCalculateCallback1 Start", Thread. CurrentThread. ManagedThreadId );

For (int I = 0; I <maxCount; I ++)

{

Thread. Sleep (sleepTimeSpan );

Console. WriteLine ("ThreadID: {0} LongCalculateCallback1 Running {1}", Thread. CurrentThread. ManagedThreadId, I );

}

Console. WriteLine ("ThreadID: {0} LongCalculateCallback1 end", Thread. CurrentThread. ManagedThreadId );

}

Private void LongCalculateCallback2 (object obj)

{

Console. WriteLine ("ThreadID: {0} LongCalculateCallback2 Start", Thread. CurrentThread. ManagedThreadId );

For (int I = 0; I <maxCount; I ++)

{

Thread. Sleep (sleepTimeSpan );

Console. WriteLine ("ThreadID: {0} LongCalculateCallback2 Running {1}", Thread. CurrentThread. ManagedThreadId, I );

}

Console. WriteLine ("ThreadID: {0} LongCalculateCallback2 end", Thread. CurrentThread. ManagedThreadId );

}

# Endregion

# Region IMain Member

Public void MainRun ()

{

ShowThreadPoolInfo ();

// ThreadTest ();

ThreadPoolTest ();

}

# Endregion

}

}

Running result: default information related to the thread pool

1 ThreadTest running result

2 ThreadPoolTest running result

Conclusion thread count:

Method Name

Maximum number of threads

Minimum number of threads

Average thread count

ThreadTest

115

22

71

ThreadPoolTest

18

11

17

Percentage of time taken by a thread to execute commands using a processor: (Note that 100% indicates 150%)

Method Name

Max

Minimum

Average

ThreadTest

110

0

---

ThreadPoolTest

138

0

----

ThreadPoolIt can reduce the number of threads of the application, or reduce the cpu usage of the application.Time usage.

However, when sleepTimeSpan is increased, the number of threads generated by programs using the thread pool increases. Why?

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.