Multi-Threaded Context Switches

Source: Internet
Author: User
Tags cas switches

Objective

This article is from the Fang Fei teacher "Java Concurrent programming Art" the first chapter.

The purpose of concurrent programming is to make the program run faster, but not to start more threads that will allow the program to execute as much as possible concurrently. In concurrent programming, if you want to perform tasks in multiple threads to make the program run faster, you face a lot of challenges, such as context switching problems, deadlock problems, and constrained by hardware and software resource constraints, this article is to investigate the issue of context switching.

What is context switching

Even a single-core CPU supports multithreaded execution of code, and the CPU implements this mechanism by allocating CPU time slices to each thread. The time slice is the time the CPU allocates to each thread, because the time slice is very short, so the CPU keeps switching threads executing, allowing us to feel multiple threads executing simultaneously, the time slice is generally dozens of milliseconds (ms).

The CPU loops through the time slice allocation algorithm to perform the task, and the current task executes a time slice and switches to the next task. However, the status of the previous task is saved before the switch, so that the next time you switch back to the task, you can load the state of the task again, from the time the task is saved to the reloading process, which is a context switch .

It's like we read two books at the same time, when we read a technical book in English, we found that a word didn't know, so we opened the Chinese-English dictionary, but before we put down the English books, the brain had to remember how many pages the book read, and then, after checking the words, could continue to read the book. Such a switch will affect the efficiency of reading, the same context switch will also affect the speed of multi-threaded execution.

Context Switch Code test

The following code shows the time of serial and military execution and additive operations:

 1 public class Contextswitchtest 2 {3 private static final Long Count = 10000; 4 5 public static void main ( String[] args) throws Exception 6 {7 concurrency (); 8 serial (); 9}10 one private static V OID concurrency () throws Exception12 {Long start = System.currenttimemillis (); Thread thread = NE W Thread (New Runnable () {public void run () + {+ int a = 0;18 fo         R (int i = 0; i < count; i++) {+ A + = 5;21}22}23             }); Thread.Start (); int b = 0;26 for (long i = 0; i < count; i++) 27 {28 b--;29}30 Thread.Join (); Long time = System.currenttimemillis ()-Start;32 Sys         Tem.out.println ("Concurrency:" + Time + "MS, B =" + b);}34 private static void serial () 36 {37 Long start = SYSTEM.CUrrenttimemillis (); int a = 0;39 for (long i = 0; i < count; i++) + {$ A + = 5;42          }43 int b = 0;44 for (int i = 0; i < count; i++) (b--;47}48 Long time = System.currenttimemillis ()-start;49 System.out.println ("Serial:" + Time + "MS, B =" + B + ", a =" + a); 50}51}

Modify the count value above, that is, modify the number of cycles, to see the serial run and the concurrency run time test results:

Number of Cycles Serial execution time-consuming/ms Concurrent execution time-consuming/ms Serial and Concurrency comparisons
100 million 78 50 Concurrency is about 0.5 times times faster.
10 million 10 6 Concurrency fast approx. 0.5~1 times
1 million 3 2 Almost
100,000 2 2 Almost
10,000 0 1 Almost, more than 10 execution down, overall serial slightly faster

As can be seen from the table, 100 concurrent executions accumulate the following, serial execution and concurrent execution run at the same speed as a whole, 10,000 times the following serial execution can even be said to be slightly faster. Why is concurrent execution slower than serial? This is because of the overhead of line threads creation and context switching .

Context Switches Viewed

The Vmstat command can be used to view the number of context switches under a Linux system, and here is an example of using Vmstat to view the number of context switches:

CS (context switch) indicates the number of context switches, and you can see that the context switches about 500~600 times per second.

If you want to see the length of the context switch, you can take advantage of LMBENCH3, which is a profiling tool.

How to reduce context switching

Since context switching leads to additional overhead, reducing the number of context switches can improve the efficiency of multi-threaded threads. The methods for reducing context switching are lock-free concurrent programming, CAS algorithms, using minimal threads, and using the coprocessor.

    • no lock concurrent programming . Multi-threaded competition, will cause context switching, so multithreading data, there are some ways to avoid the use of locks, such as the ID of the data according to the hash modulo segmentation, different threads processing different segments of the data
    • CAS algorithm . The Java atomic package uses the CAS algorithm to update the data without the need to lock
    • use minimal threads . Avoid creating unwanted threads, such as fewer tasks, but creating a lot of threads to handle, which can cause a lot of threads to wait
    • co-process . Multitask scheduling in a single thread and multiple tasks in a single thread

Multi-Threaded Context Switches

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.