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

Source: Internet
Author: User

Read the previous article:
Java multi-thread synchronization tutorial-BusyFlag or Lock (on)

We first develop a BusyFlag class, similar to Simaphore in C ++.

  1. Public ClassBusyFlag {
  2. Protected ThreadBusyflag =Null;
  3. Protected IntBusycount = 0;
  4. Public Synchronized VoidGetBusyFlag (){
  5. While(TryGetBusyFlag () =False){
  6. Try{
  7. Wait ();
  8. }Catch(ExceptionE ){}
  9. }
  10. }
  11. Private Synchronized BooleanTryGetBusyFlag (){
  12. If(Busyflag =Null){
  13. Busyflag =Thread. CurrentThread ();
  14. Busycount = 1;
  15. Return True;
  16. }
  17. If(Busyflag =Thread. CurrentThread ()){
  18. Busycount ++;
  19. Return True;
  20. }
  21. Return False;
  22. }
  23. Public Synchronized VoidFreeBusyFlag (){
  24. If(GetOwner () =Thread. CurrentThread ()){
  25. Busycount --;
  26. If(Busycount = 0 ){
  27. Busyflag =Null;
  28. Notify ();
  29. }
  30. }
  31. }
  32. Public Synchronized ThreadGetOwner (){
  33. ReturnBusyflag;
  34. }
  35. }

NOTE: Refer to Scott Oaks & Henry Wong Java Thread

BusyFlag has three public methods: getBusyFlag, freeBusyFlag, and getOwner, which are used to obtain the busy sign, release the busy sign, and obtain the thread that currently occupies the busy sign. It is also very simple to use this BusyFlag. You only need to call the getBusyFlag () of BusyFlag where the lock is needed. When the locked resources are used up, call the freeBusyFlag () of BusyFlag () you can. Next we will begin to transform the Account and ATM classes in the previous article, and apply the BusyFlag tool class so that only one thread can access the same Account at the same time. First, we need to transform the Account class. A BusyFlag object is built in the Account, and the Account is locked and unlocked through this flag object:

  1. ImportJava. util.Collections;
  2. ImportJava. util.HashMap;
  3. ImportJava. util.Map;
  4. ClassAccount {
  5. StringName;
  6. // Float amount;
  7. BusyFlag flag =NewBusyFlag ();
  8. // Use a Map to simulate persistent Storage
  9. Static MapStorage =New HashMap();
  10. Static{
  11. Storage. put ("John ",New Float(1000.0

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.