From the course: Advanced Java Programming
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Synchronized methods in Java - Java Tutorial
From the course: Advanced Java Programming
Synchronized methods in Java
- [Instructor] When using multi-threaded applications, problems can sometimes occur when more than one thread is inside the same method. In this example, I have a program that simulates withdrawing money from a bank account. So first, I have a class called BankAccount. This has a field called balance which represents the amount of money in the account. Then there is a second field called overdraft which represents the overdraft limit. The constructor takes a number for the overdraft as an argument. There are also methods to top up the account, debit the account, get the balance, and get the overdraft. Secondly, I have a class called ATM. This has a method called withdraw which takes two arguments. The first is the account the money is being withdrawn from and the second is the amount to be withdrawn. It then checks if the amount being withdrawn would take the money in the account over the overdraft limit. If it does, then it prints out a warning message and doesn't remove any money…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.