Java threads locking on a specific object

前端 未结 10 1034
抹茶落季
抹茶落季 2020-12-12 00:06

I have a web application and I am using Oracle database and I have a method basically like this:

public static void saveSomethingImportantToDataBase(Object t         


        
10条回答
  •  情书的邮戳
    2020-12-12 00:21

    public static void saveSomethingImportantToDataBase(Object theObjectIwantToSave) {
      synchronized (theObjectIwantToSave) {
    
          if (!methodThatChecksThatObjectAlreadyExists) {
             storemyObject() //pseudo code
          }
     // Have to do a lot other saving stuff, because it either saves everything or nothing
          commit() // pseudo code to actually commit all my changes to the database.
      }
    }
    

    The synchronized keyword locks the object you want so that no other method could access it.

提交回复
热议问题