I have a method in which many threads are accessed in parallel, which uses a class with two synchronized methods that I do not control. getObject and createNewObject. I want to be sure that I am not creating multiple objects (MyObject).
MyObject obj; public void method1() { obj = getObject("key"); if (obj == null) obj = createNewObject("key"); }
this, I think, will not work, since the thread can be paused between the get and create methods so that another thread can enter and create the object. The synchronized createNewObject method fixes this by checking if an already existing object with the name "key" exists and throws an exception in this case.
Which of the following methods will be proposed? Performance, safety and design. I heard that the double lock type (method 3) does not work? Maybe I should just use method1?
, . , ?
MyObject obj; public synchronized void method1() { obj = getObject("key"); if (obj == null) obj = createNewObject("key"); } public void method2() { obj = getObject("key"); if (obj == null) try { obj = createNewObject("key"); } catch (Exception e) { // ops, someone already created object "key" obj = getObject(); } } public void method3() { obj = getObject("key"); if (obj == null) obj = getObj("key"); } public synchronized MyObject getObj(String key) { MyObject obj = getObject(key); if (obj == null) obj = createNewObject(key); return obj; }
, , - , , , getObject() , . , "/ ", "/ ", . , . 1() .
UPDATE
2() , : obj, . , obj , , 2(), method2() .
obj
obj volatile, , ( 100% , ), getObject() , " ". getObject() , , . , obj. , obj, , .
. - , - , "".
method1, , . , , . , , , , - .
method1
/ . /. / , , .
EDIT: Double lock lockom, ,
---- ----
:
Object obj; public Object getObject() { if( obj == null ) { synchronized(this) { // or on something else if( obj == null ) { obj = createObject(); } } } return obj; } private Object createObject() { ... }
, , .
, createNewObject, 1 , -, . , createNewObject . , , , . , , , , .
, , :
private static class LazySomethingHolder { public static Something something = new Something(); } public static Something getInstance() { return LazySomethingHolder.something; }
concurrency .
Source: https://habr.com/ru/post/1784114/More articles:HashSet Starting Index Index Injector - javaMagento - $ this-> getSkinUrl () returns the wrong topic on the main page - magentoHow does "_setmode" in Visual Studio CRT interact with "std :: wcout"? - c ++SQL SMO database recovery not overwritten - powershellWMI access denied error requesting remote computer from ASP.NET - securityhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1784115/how-do-i-make-an-automatic-updater-for-my-desktop-java-application&usg=ALkJrhi8M6Mxkdap0NmWpgYp5XEkkj5G-gMenu icons are smaller than they should be - androidHow to get an Alfresco registration ticket without a user password, but with an impersonation of a user with a user principal name (UPN) - authenticationHow to format url formatting MVC form? - restЧтение файлов Excel в С# всегда в системе.__ ComObject? - c#All Articles