I have a DBApdater class that is accessed through many AsyncTasks. Each operation defined on the DB has to call a function written in the DBAdappter class called open, insert or delete from the db, and then close the DB. If one Async object has called open through an object of the DbApater how can i prevent a second AsyncTasks to call open until the first AsyncTask has called close on the DB. Could i use a lock like public static Object myLock =new Object();
and in the open mentod write
public void open()
{
synchronizaed(myLock.getClass)
{///Open the DB
}
}
public void close()
{
synchronizaed(myLock.getClass)
{///close the DB
}
notify();
}
would this code work. Basically the calling class would obtain a lock on the open menthod and release it only when close is called.
Kind Regards,
Muhammad Mateen
myLock, notmyLock.getClass().notify()is not required. AndmyLockshould definitely not bepublic.openandclose. It will not prevent for example,Thread1opens the database,Thread2modifies the database,Thread1modifies the database,Thread2closes the database.