关于用线程进行存取钱交替
来源:互联网 发布:上班 知乎 编辑:程序博客网 时间:2024/06/05 03:50
/**
* 取钱,存钱。
* 必须是存一个,取一个,存取相互
* @author suruidong
*
*/
public class DrawTest1 {
public static void main(String[] args) {
/**
* 取钱100次
* 存钱,开了三个线程,所以是300次
* ,因为是交换存取,所以最后线程会处于等待状态
*/
Accountt at=new Accountt("123456",0);
new DrawThreadd("取钱",at,800).start();
new DepoistThread("存钱1",at,800).start();
new DepoistThread("存钱2",at,800).start();
new DepoistThread("存钱3",at,800).start();
}
}
class DrawThreadd extends Thread{//定义一个取钱的线程
private Accountt account;
private double drawAmount;
public DrawThreadd(String name,Accountt account, double drawAmount) {
super(name);
this.account = account;
this.drawAmount = drawAmount;
}
public void run(){
for(int i=0;i<100;i++){
account.draw(drawAmount);
}
}
}
class DepoistThread extends Thread{//定义一个存钱的线程。
private Accountt account;
private double depositAmount;
public DepoistThread(String name,Accountt account, double depositAmount){
super(name);
this.account = account;
this.depositAmount = depositAmount;
}
public void run(){
for(int i=0;i<100;i++){
account.deposit(depositAmount);
}
}
}
class Accountt{//定义一个账户。里面是账号和资金,因为资金多少不能随便改动。所以提供了两个方法。一个取钱,一个存钱,
private String accountNo;
private double balance;
private boolean flag=false;//因为要做存取的动作,所以定义了一个标记,来规定存取的交替动作
public Accountt(String accountNo, double balance) {
super();
this.accountNo = accountNo;
this.balance = balance;
}
public String getAccountNo() {
return accountNo;
}
public void setAccountNo(String accountNo) {
this.accountNo = accountNo;
}
public double getBalance() {
return balance;
}
/**
* 定义一个同步取钱的方法
* 当标记为false表示没有钱,在此需要等待
* @param drawAmount
*/
public synchronized void draw(double drawAmount){
try {
if(!flag){
wait();
}else{
System.out.println(Thread.currentThread().getName()+"取钱"+drawAmount);
balance-=drawAmount;
System.out.println("余额为:"+getBalance());
flag=false;
notifyAll();//唤醒全部线程
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 定义一个同步的存钱方法
* 当标记为false为表示有钱。在此需要等待
* @param depositAmount
*/
public synchronized void deposit(double depositAmount){
try {
if(flag){
wait();
}else{
System.out.println(Thread.currentThread().getName()+"存钱"+depositAmount);
balance+=depositAmount;
System.out.println("余额:"+getBalance());
flag=true;
notifyAll();//唤醒全部线程
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((accountNo == null) ? 0 : accountNo.hashCode());
long temp;
temp = Double.doubleToLongBits(balance);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}//直接eclipse的快捷方式创建equal和hashCode方法
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Accountt other = (Accountt) obj;
if (accountNo == null) {
if (other.accountNo != null)
return false;
} else if (!accountNo.equals(other.accountNo))
return false;
if (Double.doubleToLongBits(balance) != Double
.doubleToLongBits(other.balance))
return false;
return true;
}
}
* 取钱,存钱。
* 必须是存一个,取一个,存取相互
* @author suruidong
*
*/
public class DrawTest1 {
public static void main(String[] args) {
/**
* 取钱100次
* 存钱,开了三个线程,所以是300次
* ,因为是交换存取,所以最后线程会处于等待状态
*/
Accountt at=new Accountt("123456",0);
new DrawThreadd("取钱",at,800).start();
new DepoistThread("存钱1",at,800).start();
new DepoistThread("存钱2",at,800).start();
new DepoistThread("存钱3",at,800).start();
}
}
class DrawThreadd extends Thread{//定义一个取钱的线程
private Accountt account;
private double drawAmount;
public DrawThreadd(String name,Accountt account, double drawAmount) {
super(name);
this.account = account;
this.drawAmount = drawAmount;
}
public void run(){
for(int i=0;i<100;i++){
account.draw(drawAmount);
}
}
}
class DepoistThread extends Thread{//定义一个存钱的线程。
private Accountt account;
private double depositAmount;
public DepoistThread(String name,Accountt account, double depositAmount){
super(name);
this.account = account;
this.depositAmount = depositAmount;
}
public void run(){
for(int i=0;i<100;i++){
account.deposit(depositAmount);
}
}
}
class Accountt{//定义一个账户。里面是账号和资金,因为资金多少不能随便改动。所以提供了两个方法。一个取钱,一个存钱,
private String accountNo;
private double balance;
private boolean flag=false;//因为要做存取的动作,所以定义了一个标记,来规定存取的交替动作
public Accountt(String accountNo, double balance) {
super();
this.accountNo = accountNo;
this.balance = balance;
}
public String getAccountNo() {
return accountNo;
}
public void setAccountNo(String accountNo) {
this.accountNo = accountNo;
}
public double getBalance() {
return balance;
}
/**
* 定义一个同步取钱的方法
* 当标记为false表示没有钱,在此需要等待
* @param drawAmount
*/
public synchronized void draw(double drawAmount){
try {
if(!flag){
wait();
}else{
System.out.println(Thread.currentThread().getName()+"取钱"+drawAmount);
balance-=drawAmount;
System.out.println("余额为:"+getBalance());
flag=false;
notifyAll();//唤醒全部线程
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 定义一个同步的存钱方法
* 当标记为false为表示有钱。在此需要等待
* @param depositAmount
*/
public synchronized void deposit(double depositAmount){
try {
if(flag){
wait();
}else{
System.out.println(Thread.currentThread().getName()+"存钱"+depositAmount);
balance+=depositAmount;
System.out.println("余额:"+getBalance());
flag=true;
notifyAll();//唤醒全部线程
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((accountNo == null) ? 0 : accountNo.hashCode());
long temp;
temp = Double.doubleToLongBits(balance);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}//直接eclipse的快捷方式创建equal和hashCode方法
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Accountt other = (Accountt) obj;
if (accountNo == null) {
if (other.accountNo != null)
return false;
} else if (!accountNo.equals(other.accountNo))
return false;
if (Double.doubleToLongBits(balance) != Double
.doubleToLongBits(other.balance))
return false;
return true;
}
}
0 0
- 关于用线程进行存取钱交替
- java线程 存取钱问题
- 两个线程交替执行
- java 线程 交替输出
- 线程交替输出
- 线程交替打印
- 线程交替输出
- 线程实现交替任务
- 两个线程交替执行
- 关于java线程的经典面试题。主线程子线程交替执行n次
- 线程交替执行的代码
- [Java] Thread 线程交替运行
- 2个线程交替打印
- 两个线程交替打印ABABAB
- 【多线程】两个线程 交替执行
- 线程交替有序执行任务
- 两个线程交替打印ABABAB
- java两个线程交替执行
- 习大大乌法双峰会:叱咤风云,传递满满中国正能量
- 重装win8.1之后要做的事
- 菜鸟学习日志2.界面布局的设计
- leetcode[38]:Count and Say
- java责任链模式
- 关于用线程进行存取钱交替
- 公用用例编写规范
- 【JS】用cookie记住用户名
- windows消息大全
- The System Integrity Management Platform
- 跟着实例学习设计模式(9)-桥接模式bridge(结构型)
- DZY Loves Modification
- Android Studio-1.2版本设置教程
- iOS开发网络篇—数据缓存NSURLCache