当前位置:网站首页>Use of thread communication
Use of thread communication
2022-06-12 15:18:00 【A_ M amu】
package demo06;
/**
* @program: Thread01
* @description:
* @author: Amur
* @create: 2022-01-05 11:10
**/
public class Test01 {
public static void main(String[] args) {
BankCard card=new BankCard(0);
TakeMoney takeMoney=new TakeMoney(card);
SaveMoney saveMoney=new SaveMoney(card);
Thread t1=new Thread(saveMoney," Zhang San ");
Thread t2=new Thread(takeMoney," Li Si ");
t1.start();
t2.start();
}
}
class TakeMoney implements Runnable{
private BankCard bankCard;
public TakeMoney(BankCard bankCard){
this.bankCard=bankCard;
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
bankCard.takeMoney(1000);
}
}
}
class SaveMoney implements Runnable{
private BankCard bankCard;
public SaveMoney(BankCard bankCard){
this.bankCard=bankCard;
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
bankCard.saveMoney(1000);
}
}
}
class BankCard {
private double balance;
private boolean flag = false;//flase: Said he had no money true Means rich
public BankCard(double balance) {
this.balance = balance;
}
// You must keep the card. There is no money in the card .
public synchronized void saveMoney(double money) {
if (flag == true) {
try {
this.wait();// Enter the waiting line
} catch (InterruptedException e) {
e.printStackTrace();
}
}
balance += money;
System.out.println(Thread.currentThread().getName()+" Put... Into the card "+money+"; The balance in the card is :"+balance);
flag=true; //
this.notify();// Wake up the thread waiting on the queue
}
public synchronized void takeMoney(double money){
if(flag==false){
try {
this.wait();// Enter the waiting line
} catch (InterruptedException e) {
e.printStackTrace();
}
}
balance-=money;
System.out.println(Thread.currentThread().getName()+" Took it out of the card "+money+"; The balance in the card is :"+balance);
flag=false;
this.notify();
}
}
Save more, get more .
package demo06;
/**
* @program: Thread01
* @description:
* @author: Amur
* @create: 2022-01-05 11:10
**/
public class Test01 {
public static void main(String[] args) {
BankCard card=new BankCard(0);
TakeMoney takeMoney=new TakeMoney(card);
SaveMoney saveMoney=new SaveMoney(card);
Thread t1=new Thread(saveMoney," Zhang San ");
Thread t2=new Thread(takeMoney," Li Si ");
Thread t3=new Thread(saveMoney," The king 2 ");
Thread t4=new Thread(takeMoney," Pockmarks ");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class TakeMoney implements Runnable{
private BankCard bankCard;
public TakeMoney(BankCard bankCard){
this.bankCard=bankCard;
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
bankCard.takeMoney(1000);
}
}
}
class SaveMoney implements Runnable{
private BankCard bankCard;
public SaveMoney(BankCard bankCard){
this.bankCard=bankCard;
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
bankCard.saveMoney(1000);
}
}
}
class BankCard {
private double balance;
private boolean flag = false;//flase: Said he had no money true Means rich
public BankCard(double balance) {
this.balance = balance;
}
// You must keep the card. There is no money in the card .
public synchronized void saveMoney(double money) {
if (flag == true) {
try {
this.wait();// Enter the waiting line
} catch (InterruptedException e) {
e.printStackTrace();
}
}
balance += money;
System.out.println(Thread.currentThread().getName()+" Put... Into the card "+money+"; The balance in the card is :"+balance);
flag=true; //
this.notify();// Wake up the thread waiting on the queue
}
public synchronized void takeMoney(double money){
if(flag==false){
try {
this.wait();// Enter the waiting line
} catch (InterruptedException e) {
e.printStackTrace();
}
}
balance-=money;
System.out.println(Thread.currentThread().getName()+" Took it out of the card "+money+"; The balance in the card is :"+balance);
flag=false;
this.notify();
}
}
Solution : if Switch to while
package demo06;
/**
* @program: Thread01
* @description:
* @author: Amur
* @create: 2022-01-05 11:10
**/
public class Test01 {
public static void main(String[] args) {
BankCard card=new BankCard(0);
TakeMoney takeMoney=new TakeMoney(card);
SaveMoney saveMoney=new SaveMoney(card);
Thread t1=new Thread(saveMoney," Zhang San ");
Thread t2=new Thread(takeMoney," Li Si ");
Thread t3=new Thread(saveMoney," The king 2 ");
Thread t4=new Thread(takeMoney," Pockmarks ");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class TakeMoney implements Runnable{
private BankCard bankCard;
public TakeMoney(BankCard bankCard){
this.bankCard=bankCard;
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
bankCard.takeMoney(1000);
}
}
}
class SaveMoney implements Runnable{
private BankCard bankCard;
public SaveMoney(BankCard bankCard){
this.bankCard=bankCard;
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
bankCard.saveMoney(1000);
}
}
}
class BankCard {
private double balance;
private boolean flag = false;//flase: Said he had no money true Means rich
public BankCard(double balance) {
this.balance = balance;
}
// You must keep the card. There is no money in the card .
public synchronized void saveMoney(double money) {
while (flag == true) {
try {
this.wait();// Enter the waiting line
} catch (InterruptedException e) {
e.printStackTrace();
}
}
balance += money;
System.out.println(Thread.currentThread().getName()+" Put... Into the card "+money+"; The balance in the card is :"+balance);
flag=true; //
this.notify();// Wake up the thread waiting on the queue
}
public synchronized void takeMoney(double money){
while(flag==false){
try {
this.wait();// Enter the waiting line
} catch (InterruptedException e) {
e.printStackTrace();
}
}
balance-=money;
System.out.println(Thread.currentThread().getName()+" Took it out of the card "+money+"; The balance in the card is :"+balance);
flag=false;
this.notify();
}
}
Solution : Use notifyAll Replace notify
package demo06;
/**
* @program: Thread01
* @description:
* @author: Amur
* @create: 2022-01-05 11:10
**/
public class Test01 {
public static void main(String[] args) {
BankCard card=new BankCard(0);
TakeMoney takeMoney=new TakeMoney(card);
SaveMoney saveMoney=new SaveMoney(card);
Thread t1=new Thread(saveMoney," Zhang San ");
Thread t2=new Thread(takeMoney," Li Si ");
Thread t3=new Thread(saveMoney," The king 2 ");
Thread t4=new Thread(takeMoney," Pockmarks ");
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class TakeMoney implements Runnable{
private BankCard bankCard;
public TakeMoney(BankCard bankCard){
this.bankCard=bankCard;
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
bankCard.takeMoney(1000);
}
}
}
class SaveMoney implements Runnable{
private BankCard bankCard;
public SaveMoney(BankCard bankCard){
this.bankCard=bankCard;
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
bankCard.saveMoney(1000);
}
}
}
class BankCard {
private double balance;
private boolean flag = false;//flase: Said he had no money true Means rich
public BankCard(double balance) {
this.balance = balance;
}
// You must keep the card. There is no money in the card .
public synchronized void saveMoney(double money) {
while (flag == true) {
try {
this.wait();// Enter the waiting line
} catch (InterruptedException e) {
e.printStackTrace();
}
}
balance += money;
System.out.println(Thread.currentThread().getName()+" Put... Into the card "+money+"; The balance in the card is :"+balance);
flag=true; //
this.notifyAll();// Wake up the thread waiting on the queue
}
public synchronized void takeMoney(double money){
while(flag==false){
try {
this.wait();// Enter the waiting line
} catch (InterruptedException e) {
e.printStackTrace();
}
}
balance-=money;
System.out.println(Thread.currentThread().getName()+" Took it out of the card "+money+"; The balance in the card is :"+balance);
flag=false;
this.notifyAll();
}
}
边栏推荐
- Understanding of Odom coordinate system
- Rust tip - running the tensorrt model through FFI programming
- The process of generating strong association rules from frequent itemsets
- Use of boost:: bind() in ROS
- POSTMAN-REST Client插件的应用
- C main function
- [spark][core] what is an external shuffle service?
- leetcode每日一题-公平的糖果棒交换
- PTA:自测-3 数组元素循环右移问题 (20分)
- Qiming cloud sharing | demonstrate the switch through an example of the matter protocol to control the light on and off through the matter protocol
猜你喜欢
SQL cross database injection
[jvm learning] class loading subsystem
[LDA] rough version notes of EM variational reasoning [to be improved
Deepin20.6 RTX3080 安裝顯卡驅動510.60.02、CUDA11.6、PyTorch1.11
Swap numbers, XOR, operator correlation
简单的爬虫框架:解析51job页面岗位信息
[jvm learning] parental delegation mechanism and PC register (program counter)
New关键字、引用&与指针的学习记录
C main function
[jvm learning] local method stack and heap
随机推荐
ssm中的文件上传和下载
Learning is an inhumane thing (becoming an expert's internal mind skill)
C data type
How to set public IP access on the H3C gr5200 router
[SPARK][CORE] 面试问题之谈一谈Push-based shuffle
MySQL开发注意事项(阿里巴巴开发手册)
分布式并发重复提交问题
同花顺手机炒股开户安全吗
C escape character
[jvm learning] local method stack and heap
关于互联网大厂裁员
如何给域名前加上 www
增加mysql的最大连接数
Browser fingerprint interpretation
机器人前行、旋转的service编写
ShardingSphere实践(6)——弹性伸缩
[jvm learning] types of GC and allocation process of objects on JVM heap
Rust小技巧 - 通过FFI编程运行tensorrt模型
Pta: self test -3 array element cyclic right shift problem (20 points)
Pointer related concepts