Get started with java-17.4 sync (3)-object lock

Source: Internet
Author: User

Get started with java-17.4 sync (3)-object lock

Next we will discuss the problem in the previous chapter and provide a solution: Object lock.

1. What is an object lock?

An Object lock is an exclusive lock when Java locks the Object specified by the synchronized (Object) Statement in the critical section.

2. What is a critical section?

A critical section refers to a code segment in a program. In this code, a separate concurrent thread accesses the same object. In Java, the keyword "synchronized" is used to identify a critical section.

3. Common Object locks: synchronized and ReentrantLock

The following two code examples are provided: (the two examples are modified based on the Code of the previous Bank. They mainly modify the code of the Bank, while others remain unchanged ):

Modified code:

Use synchronized

Package com. ray. ch17; public class Bank {private final double [] accounts; public double [] getAccounts () {return accounts;} public Bank (int n, double initBalance) {accounts = new double [n]; for (int I = 0; I <accounts. length; I ++) {accounts [I] = initBalance ;}} public double getTotal () {double total = 0; for (int I = 0; I <accounts. length; I ++) {total + = accounts [I];} return total;} public synchronized void transfer (int fromAccount, int toAccount, double money) {if (accounts [fromAccount] <money) {return;} accounts [fromAccount]-= money; System. out. printf ("transferred from" + fromAccount + "account % 10.2f RMB,", money); accounts [toAccount] + = money; System. out. printf ("transferred from" + toAccount + "account to % 10.2f RMB,", money); System. out. printf ("Total: % 10.2f RMB", getTotal (); System. out. println ();} public int size () {return accounts. length ;}}
Use ReentrantLock:
Package com. ray. ch17; import java. util. concurrent. locks. reentrantLock; public class Bank {private final double [] accounts; private ReentrantLock reentrantLock = new ReentrantLock (); public double [] getAccounts () {return accounts;} public Bank (int n, double initBalance) {accounts = new double [n]; for (int I = 0; I <accounts. length; I ++) {accounts [I] = initBalance ;}} public double getTotal () {double total = 0; for (int I = 0; I <accounts. length; I ++) {total + = accounts [I];} return total;} public void transfer (int fromAccount, int toAccount, double money) {reentrantLock. lock (); try {if (accounts [fromAccount] <money) {return;} accounts [fromAccount]-= money; System. out. printf ("transferred from" + fromAccount + "account % 10.2f RMB,", money); accounts [toAccount] + = money; System. out. printf ("transferred from" + toAccount + "account to % 10.2f RMB,", money); System. out. printf ("Total: % 10.2f RMB", getTotal (); System. out. println ();} finally {reentrantLock. unlock () ;}} public int size () {return accounts. length ;}}

Test code output:

Transfer RMB 6853.31 from 16 accounts and RMB 6853.31 from 80 accounts, total: RMB 1000000.00
Transfer RMB 819.37 from 5 accounts and RMB 819.37 from 92 accounts, total: RMB 1000000.00
Transfer RMB 1278.62 from 12 accounts and RMB 1278.62 from 67 accounts, total: RMB 1000000.00
Transfer RMB 1353.74 from 3 accounts and RMB 1353.74 from 9 Accounts, total: RMB 1000000.00
Transfer RMB 2316.07 from 94 accounts and RMB 2316.07 from 83 accounts, total: RMB 1000000.00
Transfer RMB 2563.51 from 59 accounts and RMB 2563.51 from 90 accounts, total: RMB 1000000.00
Transfer RMB 6276.89 from 82 accounts and RMB 6276.89 from 30 accounts, total: RMB 1000000.00
Transfer RMB 6175.01 from 2 accounts and RMB 6175.01 from 80 accounts, total: RMB 1000000.00
Transfer RMB 5030.61 from 21 accounts and RMB 5030.61 from 80 accounts, total: RMB 1000000.00

... (And so on)

The output shows that the total number is no longer changed and there is no error.

Summary: This chapter mainly discusses Synchronization Methods: Object locks and common two Object locks.

This chapter is here. Thank you.

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.