Multithreading thread safety

First, paste the original blogger's website, thank the original blogger for sharing https://home.cnblogs.com/u/lwbqqyumidi

I learned a lot from the content, and after understanding it, I edited it as follows:

What is thread safety?
When a thread is executing, it loses cpu resources, and the program executes another thread, and the thread execution body or a piece of code cannot be interrupted, and the interruption will cause problems, which will appear. The case of thread unsafety
can be understood by looking at the following example:

 1 class Account {
 2 
 3     private String accountNo;//账号
 4     private double balance;//余额
 5 
 6     public Account() {
 7 
 8     }
 9 
10     public Account(String accountNo, double balance) {
11         this.accountNo = accountNo;
12         this.balance = balance;
13     }
14 
15     public String getAccountNo() {
16         return accountNo;
17     }
18 
19     public void setAccountNo(String accountNo) {
20         this.accountNo = accountNo;
21     }
22 
23     public double getBalance() {
24         return balance;
25     }
26 
27     public void setBalance(double balance) {
28         this.balance = balance;
29     }
30 
31 }
 1 class DrawMoneyRunnable implements Runnable {
 2     private Account account;
 3     private double drawAmount;//取钱数
 4     public DrawMoneyRunnable(Account account, double drawAmount) {
 5         super();
 6         this.account = account;
 7         this.drawAmount = drawAmount;
 8     }
 9     //取钱
10     public void run(){
11                 if (account.getBalance() >= drawAmount) {
12                     System.out.println("取钱人:"+Thread.currentThread().getName()
13                             +"取钱成功, 取出钱数为:" + drawAmount);
14                     double balance = account.getBalance() - drawAmount;
15                     account.setBalance(balance);
16                     System.out.println("余额为:" + balance);
17                 }
18     }
19 }
 1 public class ThreadTest {
 2     public static void main(String[] args) {
 3         Account account = new Account("123456", 1000);
 4         DrawMoneyRunnable drawMoneyRunnable = new DrawMoneyRunnable(account, 700);
 5         Thread myThread1 = new Thread(drawMoneyRunnable);
 6         Thread myThread2 = new Thread(drawMoneyRunnable);
 7         myThread1.start();
 8         myThread2.start();
 9     }
10 }

As a result of the operation, it is found that there is a problem, because the code in the thread execution body must be executed uninterruptedly, so that the thread is not safe....

1. Synchronization method

public synchronized void run(){

}

The synchronized keyword is added to the method definition for accessing the shared resource (account object), so that this method is called a synchronized method. It can be simply understood that this method is locked, and the lock object is the object itself where the current method is located (refer to the thread object?). In a multi-threaded environment, when this method is executed, the synchronization lock must be obtained first (and at most one thread can obtain it at the same time), and the lock object will be released only after the thread executes the synchronization method. Refers to other objects created by the thread class?) It is possible to acquire this synchronization lock, and so on...

In the above example, the shared resource is the account object. When using the synchronized method, the thread safety problem can be solved. Just add the synshronized keyword before the run() method

The above paragraph was written by the original blogger, and I don't understand it very well. As long as you simply remember to lock, the method will be executed uninterruptedly....

2. Synchronized code blocks

Sometimes all you need is a piece of code to run uninterrupted, this is called a synchronized code block

synchronized (obj){

}

Among them, obj is the lock object. In general, this shared resource object is selected as the lock object.

In the above example, the shared resource is the account object. Because they are all taking money from the same account, it is best to use the account object as the lock object.

You can also use this, which refers to the thread object that executes the run method

1  public  void run(){
 2          // loop to withdraw money 
3          for ( int i = 0; i < 1000; i++ ) {
 4              synchronized ( this ){ // lock 
5                           ......
 6              }
 7      }
 8 }

3. There are other waiting, I will study it after I wake up...

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324637032&siteId=291194637