There are two mechanisms to prevent code blocks from being disturbed by concurrent access:
1, one is to use the Synchronized keyword.
2, use Reentrantlock class. (Synchronization is achieved by displaying the definition of a synchronization lock object.) )
The Synchronous Lock (Lock) method is a tool that controls the access of multiple threads to shared resources. Typically, a lock provides exclusive access to a shared resource, and only one thread can lock the lock object at a time, and the thread obtains the lock object before it begins accessing the shared resource.
Lock, Readwritelock (read-write lock) is the two root interface provided by Java 5. Do not provide a reentrantlock (Reentrant lock) implementation class for the lock interface, and a Reentrantreadwritelock implementation class for the Readwritelock interface.
In the implementation of thread-safe control, the more commonly used is reentrantlock (can be re-entry lock). Use this lock object to display a lock and release lock.
1 ImportJava.util.*;2 ImportJava.util.concurrent.locks.ReentrantLock;3 Public classOtherextendsthread{4 PrivateAccount account ;5 Private DoubleDrawamount;6 //private String name;7 PublicOther (account account,Doubledrawamount,string name) {8 Super(name);//must be the first statement9 This. account=Account ;Ten This. drawamount=Drawamount; One start (); A } - Public voidrun () { - for(inti=0;i<15;i++){ the Account.draw (drawamount); - } - } - Public Static voidMain (string[] args) { +Account A=NewAccount ("Liu Teng", 10000); -Other o=NewOther (a,1000, "a"); +Thread t=NewThread (NewRunnableclass (a)); AT.setname ("B"); at T.start (); - } - } - classaccount{ - //Defining Lock Objects - Private FinalReentrantlock lock=NewReentrantlock (); in PrivateString name; - Private DoubleMoney ; to PublicAccount () {} + PublicAccount (String name,DoubleMoney ) { - This. name=name; the This. money=Money ; * } $ Public DoubleGetmoney () {Panax Notoginseng Lock.lock (); - Try{ the returnMoney ;} + finally{ A Lock.unlock (); the } + } - Public voidDrawDoubleDrawamount) { $ Lock.lock (); $ Try{ - if(money>=Drawamount) { -SYSTEM.OUT.PRINTLN (name + "Withdraw money success!") Spit out the money: "+drawamount); the Try{ -Thread.Sleep (1000);Wuyi}Catch(Exception e) { the System.out.println (e); - } Wumoney-=Drawamount; -System.out.println ("Balance:" +Getmoney ()); About}Else{ $System.out.println ("The money failed!") Insufficient balance! "); - } -}finally{ - Lock.unlock (); A } + } the } - classRunnableclassImplementsrunnable{ $ PrivateAccount account ; the //private double drawamount; the PublicRunnableclass (account account) { the This. account=Account ; the //This.drawamount=drawamount; - } in Public voidrun () { the for(intk=0;k<10;k++){ the Try{ AboutThread.Sleep (1000); theSystem.out.println (Thread.CurrentThread (). GetName () + "read money:" +Account.getmoney ());} the Catch(Exception e) {} the } + } -}
Importing import java.util.concurrent.locks.ReentrantLock is required;
Sync Lock (Lock)