Java thread learning-synchronous code block for Thread Synchronization

Source: Internet
Author: User

 

Simulate a scenario where two people get money from the same account at the same time

 

Package cn. xy. Thread;

Public class Account
{
/**
* Account No.
*/
Private String accountNo;
/**
* Account balance
*/
Private double balance;

Public Account ()
{
Super ();
}

Public Account (String accountNo, double balance)
{
Super ();
This. accountNo = accountNo;
This. balance = balance;
}

@ Override
Public int hashCode ()
{
Return accountNo. hashCode ();
}

@ Override
Public boolean equals (Object obj)
{
If (null! = Obj & obj. getClass () = Account. class)
{
Account target = (Account) obj;
Return target. accountNo. equals (accountNo );
}
Return false;
}

/*************************************** ************************************/

Public String getAccountNo ()
{
Return accountNo;
}

Public void setAccountNo (String accountNo)
{
This. accountNo = accountNo;
}

Public double getBalance ()
{
Return balance;
}

Public void setBalance (double balance)
{
This. balance = balance;
}
}


 

Package cn. xy. Thread;

Public class DrawThread extends Thread
{
/**
* Simulate an account
*/
Private Account ac;

/**
* The amount of money that the thread wants to get
*/
Private double drawAmount;

Public DrawThread (String name, Account ac, double drawAmount)
{
Super (name );
This. ac = ac;
This. drawAmount = drawAmount;
}

@ Override
Public void run ()
{
// The account to be operated at the same time is used as the synchronization monitor. The code in synchronized {} is called the synchronization code block.
Synchronized (ac)
{
If (ac. getBalance ()> = drawAmount)
{
System. out. println (getName () + "successful banknote removal" + drawAmount );
Ac. setBalance (ac. getBalance ()-drawAmount );
System. out. println ("the balance is" + ac. getBalance ());
}
Else
{
System. out. println ("insufficient balance ");
}
}
}
}


 

Package cn. xy. Thread;

Public class Test
{
Public static void main (String [] args)
{Www.2cto.com
Account ac = new Account ("00000001", 1000 );
Thread t1 = new Thread (new DrawThread ("Lily", ac, 800 ));
Thread t2 = new Thread (new DrawThread ("Tom", ac, 800 ));
T1.start ();
T2.start ();
}
}


 

 

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.