Java multi-thread synchronization tutorial-BusyFlag or Lock (on)

Source: Internet
Author: User

The Java language has built-in synchronized keywords for multi-threaded synchronization, which greatly facilitates the compilation of multi-threaded programs in Java. However, the use of the synchronized keyword alone cannot meet all the requirements for multi-thread synchronization. As you know, synchronized can only synchronize methods or code blocks. If an application needs to synchronize across multiple methods, synchroinzed cannot be used. There are many synchronization mechanisms in C ++, such as semaphores, mutex, and temporary zone. In Java, we can also build such a synchronization tool at a higher level based on synchronized features to facilitate our use.
Currently, a Java synchronization toolkit compiled by Doug Lea is widely used. You can learn more about this package here:
Http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html
This toolkit is under the control of JCP as JSR166 and will be a formal component of JDK1.5. This article does not describe this toolkit in detail, but introduces a variety of synchronization mechanisms, and provides examples of implementation of such synchronization mechanisms, which is not an industrial implementation. However, we will refer to some code snippets of industrial implementation in this synchronization package of Doug Lea.
In this example, we still use the Account class in the previous article. However, we will write a new ATM class to simulate the ATM. Using an ATM tester class, we will generate 10 ATM threads, john's account is also queried, withdrawn, and deposited. The Account class has been modified to meet the needs of this article:

  1. ImportJava. util.HashMap;
  2. ImportJava. util.Map;
  3. ClassAccount {
  4. StringName;
  5. // Float amount;
  6. // Use a Map to simulate persistent Storage
  7. Static MapStorage =New HashMap();
  8. Static{
  9. Storage. put ("John ",New Float(1000.0f ));
  10. Storage. put ("Mike ",New Float(8000000f ));
  11. }
  12. PublicAccount (StringName ){
  13. // System. out. println ("new account:" + name );
  14. This. Name = name;
  15. // This. amount = (Float) storage. get (name). floatValue ();
  16. }
  17. Public Synchronized VoidDeposit (FloatAmt ){
  18. FloatAmount = ((Float) Storage. get (name). floatValue ();
  19. Storage. put (name,New Float(Amount + amt ));
  20. }
  21. Public Synchronized VoidWithdraw (FloatAmt)ThrowsInsufficientBalanceException {
  22. FloatAmount = ((Float) Storage. get (name). floatValue ();
  23. If(Amount> = amt)
  24. Amount-= amt;
  25. Else 
  26. Throw NewInsufficientBalanceException ();
  27. Storage. put (name,New Float(Amount ));
  28. }
  29. Public FloatGetBalance (){
  30. FloatAmount = ((Float) Storage. get (name). floatValue ();
  31. ReturnAmount;
  32. }
  33. }


In the new Account class, we use a HashMap to store Account information. The Account is used by the ATM class after logging on through login:

  1. Public ClassATM {
  2. Account acc;
  3. // As a demo, password verification is omitted
  4. Public BooleanLogin (StringName ){
  5. If(Acc! =Null)
  6. Throw New Html "target =" _ blank ">IllegalArgument

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.