I know synchronized
keyword makes method run only on single class at a time. But here is the problem.
I have a database class with methods e.g. insertAccount
, updateSetting
, etc. If I make insertAccount
, updateSetting
synchronized, each of them will be able to run only on one thread at a time.
If there was one method for whole database, it would be great, but there are not one. If one thread calls insertAccount
and another thread calls updateSetting
at the same time, it will go bad, right?
Because only one of these methods can be run at any time. So what do I do?
Is there a way to apply something like synchronized
to the whole class? So that if 1st thread calls insertAccount
and 2nd thread calls updateSetting
at the same time, 2nd thread has to wait until 1st thread finishes accessing database.