Near the reading of "in-depth understanding of parallel programming," is C, the front of the 200-page feeling is a general idea, the previous mentioned there is a counter problem, so they think of Java to achieve the next.
Problems encountered in the middle:
1. Suppose that there are only two threads, read and write, do you need to lock this resource?
2. Changes in thread status
Let's start with the code:
Public classTest {Private intcounts = 0;//This is the total number of statistics Private intinci_1 = 0;//This is a variable of thread 1. Private intinci_2 = 0;//This is a variable of thread 2. Private intInci_3 = 0;//This is a variable of thread 3. Private BooleanA =false, B =false, C =false; /** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method StubTest T =NewTest (); INC1 inc1= T.NewInc1 (); INC2 INC2= T.NewInc2 (); Inc3 inc3= T.NewInc3 (); Countall C= T.NewCountall (); Thread T1=NewThread (INC1); Thread T2=NewThread (INC2); Thread T3=NewThread (INC3); Thread CT=NewThread (c); T1.start (); T2.start (); T3.start (); Ct.start (); } //Thread 1 classInc1ImplementsRunnable { Public voidrun () { for(inti = 0; i < 300000000; i++) {inci_1++; } A=true; } } //Thread 2 classInc2ImplementsRunnable { Public voidrun () { for(inti = 0; i < 300000000; i++) {inci_2++; } b=true; } } //Thread 3 classInc3ImplementsRunnable { Public voidrun () { for(inti = 0; i < 300000000; i++) {Inci_3++; } C=true; } } //Statistical Device classCountallImplementsRunnable {@Override Public voidrun () {//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); } }}
Then, the time to solve the problem comes: excavator technology in the end which strong?????? (Pull it, everybody don't mind, please automatically ignore, just to show that LZ is a db)
The middle idea is that each thread is assigned to a space, the threads do not interact with each other, and only a single statistic is needed to update all the work. The point is that there is no lock.
This is described in "in-depth understanding of parallel programming":
If a thread is going to manipulate a locked variable, it must first be loaded into the CPU and then modified. If there is another thread at this time, the thread 1 needs to send something through the CPU's system interconnect to thread 2. Thread 1 for the erase operation. Then thread 2 is then modified. That is, this multithreading efficiency is not as good as a single occurrence
Then come back, why do we need to lock and unlock?
In addition, this thread between the state of the method is to think out, because the company computer without mouse, not Internet (new employees), can only be so much, such as copy home and then send.
1. Legitimate parallel threads can increase the state space of the program, which makes the program difficult to understand and reduce productivity
2. Assuming that I have 10,000 things to do and 100 threads, then I need to give the thread every 100 of the allotted amount, which is both efficient and resource-balanced.
3. The most important consideration in writing parallel software is how to split the correct segmentation problem to make the solution simple, scalable and high performance, while the inappropriate segmentation problem will result in a slow and complex solution.
4. When lock contention must be reduced and synchronization overhead is not the primary limitation, data locks can be used---data locks reduce the competition by spreading a large, critical section across small critical sections. However, this extensibility increases the complexity and adds additional data structures.
Record them for inspection.
A multi-threaded counter