gpt4 book ai didi

java - 使用 synchronized 关键字和 join() 的输出差异

转载 作者:行者123 更新时间:2023-11-30 09:15:37 25 4
gpt4 key购买 nike

我有 2 个类(class),

public class Account {
private int balance = 50;

public int getBalance() {
return balance;
}
public void withdraw(int amt){
this.balance -= amt; } }

public class DangerousAccount implements Runnable{
private Account acct = new Account();

public static void main(String[] args) throws InterruptedException{
DangerousAccount target = new DangerousAccount();
Thread t1 = new Thread(target);
Thread t2 = new Thread(target);

t1.setName("Ravi");
t2.setName("Prakash");
t1.start();
/* #1 t1.join(); */
t2.start();
}

public void run(){
for(int i=0; i<5; i++){
makeWithdrawl(10);
if(acct.getBalance() < 0)
System.out.println("Account Overdrawn");
}
}
public void makeWithdrawl(int amt){
if(acct.getBalance() >= amt){
System.out.println(Thread.currentThread().getName() + " is going to withdraw");
try{
Thread.sleep(500);
}catch(InterruptedException e){
e.printStackTrace();
}
acct.withdraw(amt);
System.out.println(Thread.currentThread().getName() + " has finished the withdrawl");
}else{
System.out.println("Not Enough Money For " + Thread.currentThread().getName() + " to withdraw");
}
}
}

我尝试在 makeWithdrawl 方法中添加 synchronized 关键字

public synchronized void makeWithdrawl(int amt){

而且我尝试了很多次都不断得到这个输出

Ravi is going to withdraw
Ravi has finished the withdrawl
Ravi is going to withdraw
Ravi has finished the withdrawl
Ravi is going to withdraw
Ravi has finished the withdrawl
Ravi is going to withdraw
Ravi has finished the withdrawl
Ravi is going to withdraw
Ravi has finished the withdrawl
Not Enough Money For Prakash to withdraw
Not Enough Money For Prakash to withdraw
Not Enough Money For Prakash to withdraw
Not Enough Money For Prakash to withdraw
Not Enough Money For Prakash to withdraw

这表明只有线程 t1 在工作...

如果我取消注释这行

t1.join();

并从 makeWithdrawl 方法中删除同步关键字,我得到相同的输出。

如果我不使用 synchronize 关键字或 join() 我会得到各种输出,例如

Ravi is going to withdraw
Prakash is going to withdraw
Prakash has finished the withdrawl
Ravi has finished the withdrawl
Prakash is going to withdraw
Ravi is going to withdraw
Prakash has finished the withdrawl
Ravi has finished the withdrawl
Prakash is going to withdraw
Ravi is going to withdraw
Prakash has finished the withdrawl
Ravi has finished the withdrawl
Account Overdrawn
Account Overdrawn
Not Enough Money For Ravi to withdraw
Account Overdrawn
Not Enough Money For Prakash to withdraw
Account Overdrawn
Not Enough Money For Ravi to withdraw
Account Overdrawn
Not Enough Money For Prakash to withdraw
Account Overdrawn

那么 synchronized 的输出与 join() 有何不同?我们是否需要将 synchronized 关键字添加到 run() ?

最佳答案

synchronized 将不允许其他线程同时访问同一资源。

synchronized method(){
allowed one Thread at a time.
}

join 会等待子线程完成执行。

Parent Thread started.

Child Thread started

Parent Thread is Completed but waiting for Child Thread to complete .
Child Thread Completed
Parent Thread Completed

关于java - 使用 synchronized 关键字和 join() 的输出差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19832605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com