Java review-Synchronizing code blocks

Source: Internet
Author: User

Package mytread;
public class Bank extends thread{
Private double drawamount;
Private account Account;
Public Bank (String coun_num,account account,double drawamount) {
Super (Coun_num);
This.account=account;
This.drawamount=drawamount;
}
public void Run () {
With account as the synchronization monitor, any thread entering the following code block must first obtain a lock
Other threads cannot acquire a lock and cannot decorate it, which is in line with the logic of modifying the lock and releasing the lock.
Synchronized (account) {
if (Account.get_money () >0) {
System.out.println ("Withdraw money Success" + "+" withdrawal Number: "+" "+drawamount");
}else{
System.out.println ("Failed to take money");
}

System.out.println ("Account balance:" + "" +account.get_money ());
}
}
}

Synchronizing code blocks:

Synchronization method:

Thread-Safe classes have the following characteristics through the use of synchronous methods that can be easily implemented in a thread-safe class

Objects of this class can be accessed by multiple threads safely

Each thread calls the object to get the correct method, and after each thread calls the object's arbitrary method, the object remains in a reasonable state.

Package mytread;

Import Java.math.BigDecimal;

public class Mybank extends thread{
Private acount Myacount;
Private BigDecimal Money_get;
Public Mybank () {}
Public Mybank (String name,acount myacount,bigdecimal money_get) {
Super (name);
This.myacount=myacount;
This.money_get=money_get;
}
public static void Main (string[] args) {
Acount lee=new acount ("Lee", New BigDecimal ("10000"));
for (int i = 0;i<1000;i++) {
Mybank mybank=new Mybank ("Lilang" +i, Lee, New BigDecimal ("50"));
Mybank mybank2=new Mybank ("Lilang" +i, Lee, New BigDecimal ("40"));
Mybank.start ();
Mybank2.start ();
}

}
public void Run () {
try {
Myacount.get_money (Myacount,money_get);
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

Package mytread;

Import Java.math.BigDecimal;

public class Acount {
Private String account_name;
Private BigDecimal money_sum;
Public acount () {}
Public acount (String account_name,bigdecimal money_sum) {
This.account_name=account_name;
This.money_sum=money_sum;
}
Public synchronized void Get_money (acount acount,bigdecimal money_get) throws exception{
if (Acount.money_sum.doubleValue () >0) {
Acount.money_sum=acount.money_sum.subtract (Money_get);
if (Acount.money_sum.intValue () <0) {
Acount.money_sum=acount.money_sum.add (Money_get);
System.out.println (Thread.CurrentThread (). GetName () + "Insufficient account balance" + "balance:" +acount.money_sum);
Return
}
System.out.println (Thread.CurrentThread (). GetName () + "withdrawals:" +money_get);

System.out.println (Thread.CurrentThread (). GetName () + "account balance:" +acount.money_sum);
}else{
throw new Exception ("Your account balance is insufficient!") ");
}

}

}

Methods for synchronizing processes

LILANG0 withdrawals are: 50
LILANG0 account balance: 9950
LILANG0 withdrawals are: 40
LILANG0 account balance: 9910
LILANG1 withdrawals are: 50
Lilang1 account balance: 9860
LILANG1 withdrawals are: 40
Lilang1 account balance: 9820
Lilang2 withdrawals are: 50
Lilang2 account balance: 9770
LILANG3 withdrawals are: 50
Lilang3 account balance: 9720
Lilang3 withdrawals are: 40
Lilang3 account balance: 9680
Lilang4 withdrawals are: 40
Lilang4 account balance: 9640
LILANG5 withdrawals are: 50
Lilang5 account balance: 9590
Lilang6 withdrawals are: 50
Lilang6 account balance: 9540
Lilang6 withdrawals are: 40
Lilang6 account balance: 9500
Lilang7 withdrawals are: 50
Lilang7 account balance: 9450
Lilang7 withdrawals are: 40
Lilang7 account balance: 9410
LILANG8 withdrawals are: 50
Lilang8 account balance: 9360
LILANG9 withdrawals are: 40
LILANG9 account balance: 9320
LILANG8 withdrawals are: 40
Lilang8 account balance: 9280
LILANG10 withdrawals are: 50
LILANG10 account balance: 9230
LILANG10 withdrawals are: 40
LILANG10 account balance: 9190
LILANG12 withdrawals are: 50
Lilang12 account balance: 9140
LILANG11 withdrawals are: 40
LILANG11 account Balance: 9100
LILANG12 withdrawals are: 40
Lilang12 account balance: 9060
LILANG13 withdrawals are: 50
LILANG13 account balance: 9010
LILANG13 withdrawals are: 40
LILANG13 account balance: 8970
LILANG14 withdrawals are: 40
LILANG14 account balance: 8930
LILANG15 withdrawals are: 50
LILANG15 account balance: 8880
LILANG15 withdrawals are: 40
LILANG15 account balance: 8840
LILANG16 withdrawals are: 50
Lilang16 account balance: 8790
LILANG16 withdrawals are: 40
Lilang16 account balance: 8750
LILANG17 withdrawals are: 50
Lilang17 account balance: 8700
LILANG17 withdrawals are: 40
Lilang17 account balance: 8660
LILANG18 withdrawals are: 40
Lilang18 account balance: 8620
Lilang19 withdrawals are: 40
Lilang19 account balance: 8580
LILANG20 withdrawals are: 50
LILANG20 account balance: 8530
LILANG20 withdrawals are: 40
LILANG20 account balance: 8490
LILANG21 withdrawals are: 50
Lilang21 account balance: 8440
Lilang21 withdrawals are: 40
Lilang21 account balance: 8400
LILANG23 withdrawals are: 50
Lilang23 account balance: 8350
LILANG23 withdrawals are: 40
Lilang23 account balance: 8310
LILANG24 withdrawals are: 50
Lilang24 account balance: 8260
LILANG24 withdrawals are: 40
Lilang24 account balance: 8220
LILANG25 withdrawals are: 50
Lilang25 account balance: 8170
LILANG25 withdrawals are: 40
Lilang25 account balance: 8130
LILANG26 withdrawals are: 50
LILANG26 account balance: 8080
LILANG26 withdrawals are: 40
LILANG26 account balance: 8040
LILANG27 withdrawals are: 50
LILANG27 account balance: 7990
LILANG28 withdrawals are: 40
Lilang28 account balance: 7950
LILANG29 withdrawals are: 50
LILANG29 account balance: 7900
LILANG29 withdrawals are: 40
Lilang29 account balance: 7860
LILANG30 withdrawals are: 50
Lilang30 account balance: 7810
LILANG18 withdrawals are: 50
Lilang18 account balance: 7760
LILANG44 withdrawals are: 50
Lilang44 account balance: 7710
Lilang43 withdrawals are: 50
Lilang43 account balance: 7660
LILANG32 withdrawals are: 40
LILANG32 account balance: 7620
LILANG32 withdrawals are: 50
LILANG32 account balance: 7570
LILANG27 withdrawals are: 40
LILANG27 account balance: 7530
Lilang2 withdrawals are: 40
Lilang2 account balance: 7490
Lilang41 withdrawals are: 40
Lilang41 account balance: 7450
LILANG5 withdrawals are: 40
Lilang5 account balance: 7410
Lilang43 withdrawals are: 40
Lilang43 account balance: 7370
LILANG39 withdrawals are: 50
Lilang39 account balance: 7320
Lilang41 withdrawals are: 50
Lilang41 account balance: 7270
LILANG40 withdrawals are: 50
Lilang40 account balance: 7220
Lilang39 withdrawals are: 40
Lilang39 account balance: 7180
LILANG38 withdrawals are: 40
lilang38 account balance: 7140
Lilang37 withdrawals are: 50
Lilang37 account balance: 7090
Lilang37 withdrawals are: 40
Lilang37 account balance: 7050
LILANG38 withdrawals are: 50
lilang38 account balance: 7000
Lilang36 withdrawals are: 40
Lilang36 account balance: 6960
Lilang35 withdrawals are: 40
Lilang35 account balance: 6920
Lilang35 withdrawals are: 50
Lilang35 account balance: 6870
LILANG34 withdrawals are: 40
LILANG34 account balance: 6830
LILANG34 withdrawals are: 50
LILANG34 account balance: 6780
LILANG31 withdrawals are: 50
Lilang31 account balance: 6730
LILANG30 withdrawals are: 40
Lilang30 account balance: 6690
LILANG28 withdrawals are: 50
Lilang28 account balance: 6640
Lilang22 withdrawals are: 40
Lilang22 account balance: 6600
LILANG22 withdrawals are: 50
Lilang22 account balance: 6550
LILANG19 withdrawals are: 50
Lilang19 account balance: 6500
LILANG42 withdrawals are: 40
LILANG42 account balance: 6460
LILANG4 withdrawals are: 50
Lilang4 account balance: 6410
LILANG9 withdrawals are: 50
LILANG9 account balance: 6360
LILANG11 withdrawals are: 50
Lilang11 account balance: 6310
LILANG14 withdrawals are: 50
LILANG14 account balance: 6260
LILANG31 withdrawals are: 40
Lilang31 account balance: 6220
Lilang33 withdrawals are: 50
Lilang33 account balance: 6170
Lilang33 withdrawals are: 40
Lilang33 account balance: 6130
LILANG36 withdrawals are: 50
Lilang36 account balance: 6080
LILANG40 withdrawals are: 40
Lilang40 account balance: 6040
LILANG45 withdrawals are: 40
Lilang45 account Balance: 6000
LILANG42 withdrawals are: 50
LILANG42 account balance: 5950
LILANG45 withdrawals are: 50
Lilang45 account balance: 5900
Lilang44 withdrawals are: 40
Lilang44 account balance: 5860
LILANG46 withdrawals are: 50
lilang46 account balance: 5810
LILANG46 withdrawals are: 40
lilang46 account balance: 5770
LILANG47 withdrawals are: 50
lilang47 account balance: 5720
LILANG47 withdrawals are: 40
lilang47 account balance: 5680
LILANG48 withdrawals are: 50
lilang48 account balance: 5630
LILANG48 withdrawals are: 40
lilang48 account balance: 5590
LILANG49 withdrawals are: 50
lilang49 account balance: 5540
LILANG49 withdrawals are: 40
lilang49 account balance: 5500
LILANG50 withdrawals are: 50
LILANG50 account balance: 5450
LILANG50 withdrawals are: 40
LILANG50 account balance: 5410
LILANG51 withdrawals are: 50
Lilang51 account balance: 5360
LILANG51 withdrawals are: 40
Lilang51 account balance: 5320
LILANG52 withdrawals are: 50
Lilang52 account balance: 5270
LILANG52 withdrawals are: 40
Lilang52 account balance: 5230
LILANG53 withdrawals are: 50
lilang53 account balance: 5180
LILANG53 withdrawals are: 40
lilang53 account balance: 5140
LILANG54 withdrawals are: 50
lilang54 account balance: 5090
LILANG54 withdrawals are: 40
Lilang54 account balance: 5050
LILANG55 withdrawals are: 50
Lilang55 account balance: 5000
LILANG55 withdrawals are: 40
Lilang55 account balance: 4960
LILANG56 withdrawals are: 50
lilang56 account balance: 4910
LILANG56 withdrawals are: 40
lilang56 account balance: 4870
LILANG57 withdrawals are: 50
lilang57 account balance: 4820
LILANG57 withdrawals are: 40
lilang57 account balance: 4780
LILANG58 withdrawals are: 50
lilang58 account balance: 4730
LILANG58 withdrawals are: 40
lilang58 account balance: 4690
LILANG59 withdrawals are: 40
LILANG59 account balance: 4650
LILANG60 withdrawals are: 40
Lilang60 account balance: 4610
LILANG61 withdrawals are: 50
lilang61 account balance: 4560
LILANG61 withdrawals are: 40
lilang61 account balance: 4520
LILANG62 withdrawals are: 40
lilang62 account balance: 4480
LILANG63 withdrawals are: 50
lilang63 account balance: 4430
LILANG63 withdrawals are: 40
lilang63 account balance: 4390
LILANG64 withdrawals are: 50
lilang64 account balance: 4340
LILANG64 withdrawals are: 40
LILANG64 account balance: 4300
LILANG65 withdrawals are: 50
lilang65 account balance: 4250
LILANG66 withdrawals are: 50
Lilang66 account balance: 4200
LILANG65 withdrawals are: 40
lilang65 account balance: 4160
LILANG66 withdrawals are: 40
Lilang66 account balance: 4120
LILANG67 withdrawals are: 50
lilang67 account balance: 4070
LILANG67 withdrawals are: 40
lilang67 account balance: 4030
LILANG68 withdrawals are: 50
lilang68 account balance: 3980
LILANG68 withdrawals are: 40
lilang68 account balance: 3940
LILANG69 withdrawals are: 50
lilang69 account balance: 3890
LILANG70 withdrawals are: 50
Lilang70 account balance: 3840
LILANG70 withdrawals are: 40
Lilang70 account balance: 3800
LILANG71 withdrawals are: 50
lilang71 account balance: 3750
LILANG71 withdrawals are: 40
lilang71 account balance: 3710
LILANG72 withdrawals are: 50
Lilang72 account balance: 3660
LILANG72 withdrawals are: 40
Lilang72 account balance: 3620
LILANG73 withdrawals are: 50
Lilang73 account balance: 3570
Lilang73 withdrawals are: 40
Lilang73 account balance: 3530
LILANG74 withdrawals are: 50
Lilang74 account balance: 3480
Lilang74 withdrawals are: 40
Lilang74 account balance: 3440
Lilang75 withdrawals are: 50
Lilang75 account balance: 3390
LILANG59 withdrawals are: 50
LILANG59 account balance: 3340
LILANG60 withdrawals are: 50
Lilang60 account balance: 3290
LILANG69 withdrawals are: 40
lilang69 account balance: 3250
Lilang75 withdrawals are: 40
Lilang75 account balance: 3210
LILANG76 withdrawals are: 50
Lilang76 account balance: 3160
Lilang76 withdrawals are: 40
Lilang76 account balance: 3120
Lilang77 withdrawals are: 50
Lilang77 account balance: 3070
Lilang77 withdrawals are: 40
Lilang77 account balance: 3030
LILANG78 withdrawals are: 50
lilang78 account balance: 2980
LILANG78 withdrawals are: 40
lilang78 account balance: 2940
LILANG79 withdrawals are: 50
lilang79 account balance: 2890
LILANG79 withdrawals are: 40
lilang79 account balance: 2850
LILANG80 withdrawals are: 50
Lilang80 account balance: 2800
LILANG80 withdrawals are: 40
Lilang80 account balance: 2760
LILANG81 withdrawals are: 50
lilang81 account balance: 2710
LILANG82 withdrawals are: 50
lilang82 account balance: 2660
LILANG81 withdrawals are: 40
lilang81 account balance: 2620
Lilang82 withdrawals are: 40
lilang82 account balance: 2580
LILANG62 withdrawals are: 50
lilang62 account balance: 2530
LILANG83 withdrawals are: 50
lilang83 account balance: 2480
LILANG83 withdrawals are: 40
lilang83 account balance: 2440
LILANG84 withdrawals are: 50
lilang84 account balance: 2390
LILANG84 withdrawals are: 40
lilang84 account balance: 2350
LILANG85 withdrawals are: 50
lilang85 account balance: 2300
LILANG85 withdrawals are: 40
lilang85 account balance: 2260
LILANG86 withdrawals are: 50
lilang86 account balance: 2210
LILANG86 withdrawals are: 40
lilang86 account balance: 2170
LILANG87 withdrawals are: 50
lilang87 account balance: 2120
LILANG87 withdrawals are: 40
lilang87 account balance: 2080
LILANG88 withdrawals are: 50
lilang88 account balance: 2030
LILANG88 withdrawals are: 40
lilang88 Account balance: 1990
LILANG89 withdrawals are: 50
lilang89 account balance: 1940
LILANG89 withdrawals are: 40
lilang89 account balance: 1900
LILANG90 withdrawals are: 50
Lilang90 account balance: 1850
LILANG90 withdrawals are: 40
Lilang90 account balance: 1810
LILANG91 withdrawals are: 50
lilang91 account balance: 1760
LILANG91 withdrawals are: 40
lilang91 account balance: 1720
LILANG92 withdrawals are: 50
lilang92 account balance: 1670
LILANG93 withdrawals are: 50
lilang93 account balance: 1620
LILANG93 withdrawals are: 40
lilang93 account balance: 1580
LILANG94 withdrawals are: 50
lilang94 account balance: 1530
LILANG94 withdrawals are: 40
lilang94 account balance: 1490
LILANG95 withdrawals are: 50
lilang95 account balance: 1440
Lilang95 withdrawals are: 40
Lilang95 account balance: 1400
LILANG96 withdrawals are: 50
lilang96 account balance: 1350
LILANG96 withdrawals are: 40
lilang96 account balance: 1310
LILANG97 withdrawals are: 50
LILANG97 account balance: 1260
LILANG97 withdrawals are: 40
LILANG97 account balance: 1220
LILANG98 withdrawals are: 50
Lilang98 account balance: 1170
LILANG98 withdrawals are: 40
Lilang98 account balance: 1130
LILANG99 withdrawals are: 50
LILANG99 Account Balance: 1080
LILANG99 withdrawals are: 40
Lilang99 account balance: 1040
LILANG100 withdrawals are: 50
lilang100 account balance: 990
LILANG100 withdrawals are: 40
lilang100 account balance: 950
LILANG101 withdrawals are: 50
LILANG101 account balance: 900
LILANG101 withdrawals are: 40
lilang101 account balance: 860
LILANG102 withdrawals are: 50
lilang102 account balance: 810
LILANG102 withdrawals are: 40
lilang102 account Balance: 770
LILANG103 withdrawals are: 50
lilang103 Account balance: 720
LILANG103 withdrawals are: 40
lilang103 account balance: 680
LILANG104 withdrawals are: 50
lilang104 account balance: 630
LILANG104 withdrawals are: 40
lilang104 account balance: 590
LILANG105 withdrawals are: 50
lilang105 account balance: 540
LILANG105 withdrawals are: 40
lilang105 account balance: 500
LILANG106 withdrawals are: 50
lilang106 account balance: 450
LILANG106 withdrawals are: 40
lilang106 account balance: 410
LILANG107 withdrawals are: 50
lilang107 account balance: 360
LILANG107 withdrawals are: 40
lilang107 account balance: 320
LILANG108 withdrawals are: 50
lilang108 account balance: 270
LILANG108 withdrawals are: 40
lilang108 account balance: 230
LILANG110 withdrawals are: 50
lilang110 account balance: 180
LILANG111 withdrawals are: 50
lilang111 account balance: 130
LILANG110 withdrawals are: 40
lilang110 account balance: 90
LILANG109 withdrawals are: 40
lilang109 account balance: 50
LILANG111 withdrawals are: 40
lilang111 account balance: 10
lilang112 account balance Insufficient balance is: 10
lilang92 account balance Insufficient balance is: 10
lilang109 account balance Insufficient balance is: 10
lilang113 account balance Insufficient balance is: 10

Partial results

Java review-Synchronizing code blocks

Related Article

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.