head first java中的线程问题
问题:
在以下这段代码中,我们创建了两个线程来分别执行ryanandmonicajob类的run方法。每个线程对bankaccount对象进行取款操作,并在取款前检查账户余额是否大于等于取款金额。然而,输出结果却令人惊讶。
代码:
class bankaccount { private int balance = 100; public int getbalance() { return balance; } public void withdraw(int amount) { balance = balance - amount; } } public class ryanandmonicajob implements runnable { private bankaccount account = new bankaccount(); public static void main(string[] args) { ryanandmonicajob thejob = new ryanandmonicajob(); thread one = new thread(thejob); thread two = new thread(thejob); one.setname("ryan"); two.setname("monica"); one.start(); two.start(); } public void run() { for (int x = 0; x < 10; x++) { makewithdrawal(10); if (account.getbalance() < 0) { system.out.println("overdrawn!"); } } } private void makewithdrawal(int amount) { string currentthread = thread.currentthread().getname(); if (account.getbalance() >= amount) { system.out.println(currentthread + " is about to withdraw"); try { system.out.println(currentthread + " is going to sleep"); thread.sleep(500); } catch (interruptedexception ex) { ex.printstacktrace(); } system.out.println(currentthread + " woke up."); account.withdraw(amount); system.out.println(currentthread + " completes the withdrawal"); } else { system.out.println("sorry, not enough for " + currentthread); } } }
输出:
ryan is about to withdraw ryan: sorry, not enough for ryan monica: is about to withdraw ...
问题原因:
当ryan线程执行makewithdrawal方法时,它先输出"ryan is about to withdraw",然后进入sleep状态。然而,这并不是像你想象的那样交出控制权给另一个线程。相反,在这个sleep时间内,monica线程仍然继续执行,并且已经检测到账户余额不足,所以输出"ryan: sorry, not enough for ryan"。
一旦ryan线程从sleep状态中苏醒,它继续执行makewithdrawal方法。此时,账户余额已经发生了变化,并且不再足以支持取款操作。因此,它输出"monica is about to withdraw"。
解决方法:
为了避免这种不同步,我们可以在makewithdrawal方法中使用synchronized块来确保账户对象的访问是同步的。
private void makeWithdrawal(int amount) { synchronized (account) { String currentThread = Thread.currentThread().getName(); if (account.getBalance() >= amount) { System.out.println(currentThread + " is about to withdraw"); try { System.out.println(currentThread + " is going to sleep"); Thread.sleep(500); } catch (InterruptedException ex) { ex.printStackTrace(); } System.out.println(currentThread + " woke up."); account.withdraw(amount); System.out.println(currentThread + " completes the withdrawal"); } else { System.out.println("Sorry, not enough for " + currentThread); } } }
在IntelliJ IDEA中使用快捷键修改POM文件依赖版本时生成新的repository标签而不是直接修改版本号的原因可能与IDE的自动补全和依赖管理机制有关。以下是一些可能的原因和解决方法:依赖管理机制:IntelliJ IDEA可能会尝试从不同的存储库中查找指定版本的依赖。如果指定的版本在当前配置的存储库中找不到,IDE可能会自动添加新的存储库以确保可以下载到所需的版本。快捷键功能限制:某些快捷键可能只负责版本号的快速修改,而不处理存储库的管理。当你使用快捷键时,IDE可能会默认添加新的存储库以确
Java框架的优点和发展趋势是什么?
Java框架和F#框架在金融领域的优势
Java函数式编程对数据处理的革命性影响
JNA调用C++ DLL时如何避免异常导致JVM崩溃?
Android RecyclerView数据更新后视图不刷新,如何解决?