Dark Horse programmer _ multi-thread collaboration 2 (await and signal)

Source: Internet
Author: User

-------
Android training and Java training. We look forward to communicating with you! ----------

The first method to implement multi-threaded collaboration is to use the wait and notify methods mentioned by the object class. After jdk1.5, a new implementation method is available, using Java. util. concurrent. the condition interface in the locks package. The await of the conditon interface works with signal and lock to implement the second multi-thread collaboration method. The key code is as follows:

// Save Public void depositmoney (float money) {// 1. lock accountslock. lock (); // 2. if hasmoney is set to true, wait. If hasmoney is set to false, save the money. Try {If (hasmoney) {condition. await ();} else {This. balance + = money; hasmoney = true; system. out. println (thread. currentthread (). getname () + ":" + money + ", balance:" + this. balance); condition. signal () ;}} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {// unlock accountslock in finally. unlock ();}}

The Code is as follows:

Package c16thread2; import Java. util. concurrent. locks. condition; import Java. util. concurrent. locks. lock; import Java. util. concurrent. locks. reentrantlock;/*** program description knowledge point: Java. util. concurrent. lock interface and condition interface in the locks package * business logic: A bank saves money to withdraw money, a deposit thread, a money withdrawal thread, an account entity, it is required that two threads save and withdraw money cyclically for the same account, that is, save money-> * withdraw money-> save money. You cannot withdraw money twice in a row or twice in a row. Implementation principle: Use the lock class object to control thread synchronization, use the condition class to coordinate thread cooperation ** @ author renyajun **/public class lockandcondition {public static void main (string [] ARGs) {// Why must todo be finalfinal accounts account = new accounts (); New thread (New runnable () {public void run () {// todo auto-generated method stubaccount. depositmoney (800) ;}}, "Save Money "). start (); New thread (New runnable () {public void run () {// todo auto-generated method stubacco Unt. getmoney (800) ;}}, "Get money "). start () ;}} class accounts {// locks interface object, each time only one thread can lock this locks object, that is, for the same accounts object, only one thread can be accessed at a time. Lock accountslock = new reentrantlock (); // creates a condition interface object bound to the lock instance. Condition condition = accountslock. newcondition (); // balance float balance = 0; Boolean hasmoney = false; public float getbalance () {return balance;} // Save Public void depositmoney (float money) {// 1. lock accountslock. lock (); // 2. if hasmoney is set to true, wait. If hasmoney is set to false, save the money. Try {If (hasmoney) {condition. await ();} else {This. balance + = money; hasmoney = true; system. out. println (thread. currentthread (). getname () + ":" + money + ", balance:" + this. balance); condition. signal () ;}} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace ();} finally {// unlock accountslock in finally. unlock () ;}// get the public void getmoney (float money) {accountslock. lock (); try {If (hasmoney) {This. balance-= money; hasmoney = false; system. out. println (thread. currentthread (). getname () + ":" + money + ", balance:" + this. balance); condition. signal ();} else {condition. await () ;}} catch (exception e) {// todo: handle exception} finally {accountslock. unlock ();}}}

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.