Analyzes thread security from assembly, and analyzes thread security from assembly.

Source: Internet
Author: User

Analyzes thread security from assembly, and analyzes thread security from assembly.

What is thread security first?

When multiple threads access a class, no matter what scheduling method is used in the running environment or how these threads will be executed alternately, and no additional synchronization or collaboration is required in the main code, this class can show correct behavior, so long said this class is thread-safe.

Columns that are not secure in typical threads:

1 import java. util. *; 4 5 class Worker implements Runnable {6 private UnsafeCount unsafeCount; 7 8 public Worker (UnsafeCount unsafeCount) {9 this. unsafeCount = unsafeCount; 10} 11 12 @ Override13 public void run () {14 // TODO Auto-generated method stub15 for (int I = 0; I <1000; I ++) 16 unsafeCount. increase (); 17} 18 19} 20 21 public class UnsafeCount {22 private int count = 0; 23 24 public void increase () {25 count ++; 26} 27 28 public int getCount () {29 return count; 30} 31 32 public static void main (String [] args) throws InterruptedException {33 UnsafeCount uc = new UnsafeCount (); 34 35 // here, the simple list method is used to control the end of the Thread. A better implementation is to use the locking CountDownLatch or the baricbarrier36 List <Thread> list = new ArrayList <Thread> (); // 37 38 for (int I = 0; I <10; I ++) {39 Thread worker = new Thread (new Worker (uc); 40 worker. start (); 41 list. add (worker); 42} 43 44 // blocking until the Thread ends 45 for (Thread t: list) {46 t. join (); 47} 48 49 System. out. println ("total is:" + uc. getCount (); 50 51} 52}

Running results (different results ):
Total is: 7628

 

Let's analyze the result carefully and start 10 threads. Each thread performs 1000 auto-increment operations on count. The expected result is 1000*10 = 10000. Obviously, the running result is inconsistent with the expected result. The conclusion is that this class is thread insecure. Why is this happening?

The reason is that the count ++ operation is not Atomic. In fact, this auto-increment operation is a compound operation: Read-Modify-write. If we understand the assembly language, the assembler corresponding to the auto-increment operation may be:

Movl count, % eax # Read the count value into the eax register, add 1 to the value in register eax of inc % eax #, that is, rewrite the count value movl % eax, % ebx # Here, The ebx register stores the memory address of count. Here, the value writes the rewritten count value to the memory.


In this case, there is A problem. If there are two threads A and B operation variables count, the initialization time count is 0. before thread A does not write the rewrite value, for example, when thread A executes step 2, thread B starts to execute, as shown below:

 

Thread A reads the count value 0 (step 1)-> rewrite the count value to 1 (step 2)-> write the rewritten count value into the memory (Step 3)

Thread A reads the count value 0 (step 1)-> rewrite the count value to 1 (step 2)-> write the rewritten count value into the memory (Step 3)

 

Because thread A has not updated and rewritten the value of count to the memory, the value of count read from thread B is still 0, which leads to the value of count 1 after the last two threads end. We can see that two auto-increment operations are performed. The expected result is 2, but the actual result may be 1. this is also the reason why the actual result of auto-increment operations is usually smaller than the expected result when the thread is not secure.

 

In the next article, I will explain what is thread unsafe.

 

 

 

 

 

  

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.