A multi-thread counter and multi-thread counter

Source: Internet
Author: User

A multi-thread counter and multi-thread counter

I recently read "deep understanding of parallel programming", which is C. The first 200 pages seem to be a general idea. As mentioned above, there is a counter problem, so I want to implement it in java.

Problems encountered:
1. If there are only two threads, one read and one write, do you need to lock this resource?
2. Changing the thread status

Let's start with the code:

Public class Test {private int counts = 0; // This is the total number of statistics private int inci_1 = 0; // This is the variable private int inci_2 = 0 for thread 1; // This is the variable private int inci_3 = 0 for thread 2; // This is the variable private boolean a = false, B = false, c = false for thread 3; /*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub Test t = new Test (); inc1 inc1 = t. new Inc1 (); Inc2 inc2 = t. new Inc2 (); Inc3 inc3 = t. new In C3 (); CountAll c = t. new CountAll (); Thread t1 = new Thread (inc1); Thread t2 = new Thread (inc2); Thread t3 = new Thread (inc3 ); thread ct = new Thread (c); t1.start (); t2.start (); t3.start (); ct. start () ;}// thread 1 class Inc1 implements Runnable {public void run () {for (int I = 0; I <300000000; I ++) {inci_1 ++;} a = true ;}// thread 2 class Inc2 implements Runnable {public void run () {for (int I = 0; I <300000000; I ++) {inci_2 ++;} B = true ;}// thread 3 class Inc3 implements Runnable {public void run () {for (int I = 0; I <300000000; I ++) {inci_3 ++;} c = true ;}} // statistician class CountAll implements Runnable {@ Override public void run () {// TODO Auto-generated method stub while (! (A & B & c) {counts = inci_1 + inci_2 + inci_3; System. out. println (counts);} counts = inci_1 + inci_2 + inci_3; System. out. println ("the final result is:" + counts );}}}

So the time comes to solve the problem: Which of the following is the best excavator technology ?????? (Do not mind, please ignore it automatically, just to show that LZ is a DB)

 

The intermediate idea is: allocate a space to each thread, and threads will not affect each other. You only need to update all the work through a statistician. The key is that there is no lock.

Parallel Programming is described as follows:

If a thread operates on a locked variable, it must be loaded into the CPU before modification. at this time, if there is another thread for operation, thread 1 needs to send things to thread 2 through the CPU System Interconnect, and thread 1 needs to be erased. then thread 2 is modified. that is to say, this multithreading efficiency is not as high as that of a single thread.

Then we came back. Why do we still need to unlock the lock?
In addition, the way to the state between threads is self-developed, because the company's computer does not have a mouse, cannot access the Internet (new employees), can only pull so much, wait for the copy to go home and then send.

1. Valid parallel threads will increase the state space of the program, making it hard for the program to understand and reduce productivity.
2. Suppose I have 10000 things to get and 100 threads, so I need to allocate 100 of each thread. This not only takes both efficiency and resources into account.
3. the most important consideration for writing parallel software is how to split the correct splitting problem so that the solution is simple, scalable, and high-performance, however, an improper splitting problem will produce a slow and complex solution.

4. when the lock competition must be reduced and the synchronization overhead is not the main limitation, you can use data locks-data locks are used to distribute a large critical section to each small critical section to reduce the lock competition. however, this scalability increases complexity and adds additional data structures.

Record for inspection

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.