Home > Back-end >  Java implementation access using synchronized multithreaded - synchronization
Java implementation access using synchronized multithreaded - synchronization

Time:09-21

Now have the Account class, have
Properties:
Private int the balance
Methods:
The corresponding getter methods,

For the class to write:
Void deposit (int) money//save money, on the basis of the balance plus money
Void withdraw (int money)//withdraw money, on the basis of balance minus the money

Note:

If balance< when you pick up the money; 0, throws an exception, in the case of multi-threaded, if only to save a thread, but there are multiple threads to withdraw money, is likely to throw an exception,
You need to write a complete method of deposit and withdraw the first half of the code to solve the problem,
The referee test program:
import java.util.Scanner;

//here is the Account class the first part of the existing code
/* */
here is the deposit code/* this is the first half of the withdraw code */
If (balance<0)//here is the second part of the withdraw code,
Throw new an IllegalStateException (the balance + "");
}

/* system existing code without attention */

CodePudding user response:

Why let him appear less than zero, this kind of situation is not to put an end to

CodePudding user response:

This is the problem, the PTA subject to code complete methods of deposit and withdraw the first half of the code, the if (balance<0) throw new an IllegalStateException (the balance + "") of the fixed code system is given
  • Related