public class ThreadTest {
public static void main(String[] args) {
Account account = new Account("123456", 1000);
DrawMoneyRunnable drawMoneyRunnable = new DrawMoneyRunnable(account, 700);
Thread myThread1 = new Thread(drawMoneyRunnable);
Thread myThread2 = new Thread(drawMoneyRunnable);
myThread1.start();
myThread2.start();
}
}
class DrawMoneyRunnable implements Runnable {
private Account account;
private double drawAmount;
public DrawMoneyRunnable(Account account, double drawAmount) {
super();
this.account = account;
this.drawAmount = drawAmount;
}
public void run() {
if (account.getBalance() >= drawAmount) { //1
System.out.println("取钱成功, 取出钱数为:" + drawAmount);
double balance = account.getBalance() - drawAmount;
account.setBalance(balance);
System.out.println("余额为:" + balance);
}
}
}
class Account {
private String accountNo;
private double balance;
public Account() {
}
public Account(String accountNo, double balance) {
this.accountNo = accountNo;
this.balance = balance;
}
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;
}
}
public synchronized void run() {
// ....
}
synchronized (obj) {
//...
}
class X {
// 显示定义Lock同步锁对象,此对象与共享资源具有一对一关系
private final Lock lock = new ReentrantLock();
public void m(){
// 加锁
lock.lock();
//... 需要进行线程安全同步的代码
// 释放Lock锁
lock.unlock();
}
}
package com.qqyumidi;
public class ThreadTest {
public static void main(String[] args) {
Account account = new Account("123456", 0);
Thread drawMoneyThread = new DrawMoneyThread("取钱线程", account, 700);
Thread depositeMoneyThread = new DepositeMoneyThread("存钱线程", account, 700);
drawMoneyThread.start();
depositeMoneyThread.start();
}
}
class DrawMoneyThread extends Thread {
private Account account;
private double amount;
public DrawMoneyThread(String threadName, Account account, double amount) {
super(threadName);
this.account = account;
this.amount = amount;
}
public void run() {
for (int i = 0; i < 100; i++) {
account.draw(amount, i);
}
}
}
class DepositeMoneyThread extends Thread {
private Account account;
private double amount;
public DepositeMoneyThread(String threadName, Account account, double amount) {
super(threadName);
this.account = account;
this.amount = amount;
}
public void run() {
for (int i = 0; i < 100; i++) {
account.deposite(amount, i);
}
}
}
class Account {
private String accountNo;
private double balance;
// 标识账户中是否已有存款
private boolean flag = false;
public Account() {
}
public Account(String accountNo, double balance) {
this.accountNo = accountNo;
this.balance = balance;
}
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;
}
/**
* 存钱
*
* @param depositeAmount
*/
public synchronized void deposite(double depositeAmount, int i) {
if (flag) {
// 账户中已有人存钱进去,此时当前线程需要等待阻塞
try {
System.out.println(Thread.currentThread().getName() + " 开始要执行wait操作" + " -- i=" + i);
wait();
// 1
System.out.println(Thread.currentThread().getName() + " 执行了wait操作" + " -- i=" + i);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
// 开始存钱
System.out.println(Thread.currentThread().getName() + " 存款:" + depositeAmount + " -- i=" + i);
setBalance(balance + depositeAmount);
flag = true;
// 唤醒其他线程
notifyAll();
// 2
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "-- 存钱 -- 执行完毕" + " -- i=" + i);
}
}
/**
* 取钱
*
* @param drawAmount
*/
public synchronized void draw(double drawAmount, int i) {
if (!flag) {
// 账户中还没人存钱进去,此时当前线程需要等待阻塞
try {
System.out.println(Thread.currentThread().getName() + " 开始要执行wait操作" + " 执行了wait操作" + " -- i=" + i);
wait();
System.out.println(Thread.currentThread().getName() + " 执行了wait操作" + " 执行了wait操作" + " -- i=" + i);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
// 开始取钱
System.out.println(Thread.currentThread().getName() + " 取钱:" + drawAmount + " -- i=" + i);
setBalance(getBalance() - drawAmount);
flag = false;
// 唤醒其他线程
notifyAll();
System.out.println(Thread.currentThread().getName() + "-- 取钱 -- 执行完毕" + " -- i=" + i); // 3
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有