当前位置:网站首页>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();
}
}边栏推荐
- Alibaba, Tencent et pingduo sont à la recherche d'une nouvelle logique pour l'Internet industriel
- Module yaml error: Unexpected key in data: static_ context [line 9 col 3]
- ROS 中 boost::bind( ) 的使用
- The process of generating strong association rules from frequent itemsets
- Self induction of exception handling
- Qiming cloud sharing | demonstrate the switch through an example of the matter protocol to control the light on and off through the matter protocol
- Function recursion example
- ngork实现内网穿透--免费
- Pta: self test -3 array element cyclic right shift problem (20 points)
- Error 1105: message:\“raft entry is too large
猜你喜欢

Jetpack architecture component learning (3) -- activity results API usage

Deepin20.6 rtx3080 installer le lecteur de carte graphique 510.60.02, cuda 11.6, pytorch1.11

Wild pointer understanding

Two ways of array simulating queue

Change according to the situation, the road to promotion in the second half of 2022

解决log4j2漏洞遭到挖矿、僵尸进程病毒攻击

Selenium advanced

NETCORE combined with cap event bus to realize distributed transaction -- Introduction (1)

Seaborn的简述

TCP/IP 三次握手四次挥手(面试题)
随机推荐
Rust tip - running the tensorrt model through FFI programming
Wild pointer understanding
阿裏、騰訊、拼多多垂範,產業互聯網的新邏輯漸顯
Assertion of selenium webdriver
如何给域名前加上 www
关于互联网大厂裁员
[LDA] basic knowledge notes - mainly AE and VAE
Tcp/ip three handshakes and four waves (interview questions)
FIRST集与FOLLOW集白话版
解决log4j2漏洞遭到挖矿、僵尸进程病毒攻击
Scala下载及IDEA安装Scala插件(保姆级教程超详细)
ROS beginners write the server that the little turtle rotates a certain angle at a certain speed
USART(RS232422485)、I2C、SPI、CAN、USB总线
IMU的学习记录
學習是一件逆人性的事情(成為高手的內功心法)
Learning is an inhumane thing (becoming an expert's internal mind skill)
Pta: self test -2 prime pair conjecture (20 points)
C operator
Microservice fault tolerance
SQL cross database injection