求助:JAVA编程:银行存取款多线程编程,即要实现线程同步
银行存取款多线程编程:取款getbankMoney()时,要判断余额不足;存款setbankMoney()时,限制一次存款额为1.1万元;存款和取款不能同时进行,即要实现...
银行存取款多线程编程:取款getbankMoney()时,要判断余额不足;存款setbankMoney()时,限制一次存款额为1.1万元;存款和取款不能同时进行,即要实现线程同步。
展开
4个回答
展开全部
public class TestBank{
public static void main(String[] args){
String s1="saveBankMoney";
String s2="getBankMoney";
Bank bank=new Bank(s1,s2);
Thread save_Money;
Thread get_Money;
save_Money=new Thread(bank);
get_Money=new Thread(bank);
save_Money.setName(s1);
get_Money.setName(s2);
save_Money.start();
get_Money.start();//启动线程
}
}
class Bank implements Runnable{
public Bank(String s1,String s2){
this.s1=s1;
this.s2=s2;
}
public void run(){
saveBankMoney(11000);//存款额规定为11000
getBankMoney(5000);//取款5000
}
//同步方法,实现存款
public synchronized void saveBankMoney(double save_money){
if(Thread.currentThread().getName().equals(s1)){
for(int i=1;i<3;i++){
if (save_money==11000){
this.money+=save_money;
try{
Thread.sleep(2000);//休眠
}
catch(InterruptedException ex ){
ex.printStackTrace();
}
System.out.println("我要存款:"+save_money);
System.out.println("存款成功!帐号目前余额为:"+this.money);
}
}
}
}
//同步方法,实现取款
public synchronized void getBankMoney(double get_money){
if(Thread.currentThread().getName().equals(s2)){
for(int i=1;i<3;i++){
if(money>=get_money){
this.money=this.money-get_money;
try{
Thread.sleep(2000);//休眠
}
catch(InterruptedException ex){
ex.printStackTrace();
}
System.out.println("我要取款:"+get_money);
System.out.println("取款成功!帐号目前余额为:"+this.money);
}
else{
System.out.println("帐号余额不足!");
}
}
}
}
private double money=10000.0;//帐号初始额为10000
private String s1;
private String s2;
}
public static void main(String[] args){
String s1="saveBankMoney";
String s2="getBankMoney";
Bank bank=new Bank(s1,s2);
Thread save_Money;
Thread get_Money;
save_Money=new Thread(bank);
get_Money=new Thread(bank);
save_Money.setName(s1);
get_Money.setName(s2);
save_Money.start();
get_Money.start();//启动线程
}
}
class Bank implements Runnable{
public Bank(String s1,String s2){
this.s1=s1;
this.s2=s2;
}
public void run(){
saveBankMoney(11000);//存款额规定为11000
getBankMoney(5000);//取款5000
}
//同步方法,实现存款
public synchronized void saveBankMoney(double save_money){
if(Thread.currentThread().getName().equals(s1)){
for(int i=1;i<3;i++){
if (save_money==11000){
this.money+=save_money;
try{
Thread.sleep(2000);//休眠
}
catch(InterruptedException ex ){
ex.printStackTrace();
}
System.out.println("我要存款:"+save_money);
System.out.println("存款成功!帐号目前余额为:"+this.money);
}
}
}
}
//同步方法,实现取款
public synchronized void getBankMoney(double get_money){
if(Thread.currentThread().getName().equals(s2)){
for(int i=1;i<3;i++){
if(money>=get_money){
this.money=this.money-get_money;
try{
Thread.sleep(2000);//休眠
}
catch(InterruptedException ex){
ex.printStackTrace();
}
System.out.println("我要取款:"+get_money);
System.out.println("取款成功!帐号目前余额为:"+this.money);
}
else{
System.out.println("帐号余额不足!");
}
}
}
}
private double money=10000.0;//帐号初始额为10000
private String s1;
private String s2;
}
展开全部
package com.ht.bank;
import java.util.Random;
/**
* 银行账号
* @author ht
*
*/
public class Account {
//账号存款
private double deposit;
public Account(){}
public Account(double deposit){
this.deposit = deposit ;
}
public synchronized void getbankMoney(double money){
System.out.print("取款:"+ money);
if((deposit - money) <= 0 ){
System.out.println(",余额不足,当前存款:"+deposit);
}else{
//修改账号存款
deposit -= money;
System.out.println("取款成功,余额:"+deposit);
}
}
public synchronized void addBankMoney(double money){
System.out.print("存款:"+money+"元");
if(money > 11000){
System.out.println("金额超出限制,存款失败");
}else{
//增加账号存款
deposit += money;
System.out.println("当前账号余额:"+deposit);
}
}
public static void main(String[] args) {
final Account account = new Account(10000);//初始化账户 10000元
//多线程操作,开10个存款线程
for(int i = 0 ; i< 10 ;i++){
Thread thread = new Thread(new Runnable() {
public void run() {
//随机产生存款金额(0 - 1000000之间)
double money = new Random().nextInt(50000);
account.addBankMoney(money);
}
});
thread.start();
}
//多线程操作,开10个取款线程
for(int i = 0 ; i< 10 ;i++){
Thread thread = new Thread(new Runnable() {
public void run() {
//随机产生存款金额(0 - 1000000之间)
double money = new Random().nextInt(1000000);
account.getbankMoney(money);
}
});
thread.start();
}
}
}
import java.util.Random;
/**
* 银行账号
* @author ht
*
*/
public class Account {
//账号存款
private double deposit;
public Account(){}
public Account(double deposit){
this.deposit = deposit ;
}
public synchronized void getbankMoney(double money){
System.out.print("取款:"+ money);
if((deposit - money) <= 0 ){
System.out.println(",余额不足,当前存款:"+deposit);
}else{
//修改账号存款
deposit -= money;
System.out.println("取款成功,余额:"+deposit);
}
}
public synchronized void addBankMoney(double money){
System.out.print("存款:"+money+"元");
if(money > 11000){
System.out.println("金额超出限制,存款失败");
}else{
//增加账号存款
deposit += money;
System.out.println("当前账号余额:"+deposit);
}
}
public static void main(String[] args) {
final Account account = new Account(10000);//初始化账户 10000元
//多线程操作,开10个存款线程
for(int i = 0 ; i< 10 ;i++){
Thread thread = new Thread(new Runnable() {
public void run() {
//随机产生存款金额(0 - 1000000之间)
double money = new Random().nextInt(50000);
account.addBankMoney(money);
}
});
thread.start();
}
//多线程操作,开10个取款线程
for(int i = 0 ; i< 10 ;i++){
Thread thread = new Thread(new Runnable() {
public void run() {
//随机产生存款金额(0 - 1000000之间)
double money = new Random().nextInt(1000000);
account.getbankMoney(money);
}
});
thread.start();
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
找专业人士吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
二楼正解~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询