{"id":604,"date":"2024-05-25T03:28:28","date_gmt":"2024-05-25T03:28:28","guid":{"rendered":"http:\/\/emacslisp.com\/?p=604"},"modified":"2024-05-25T03:28:29","modified_gmt":"2024-05-25T03:28:29","slug":"synchronized-locks","status":"publish","type":"post","link":"http:\/\/emacslisp.com\/?p=604","title":{"rendered":"synchronized locks"},"content":{"rendered":"\n<p><strong>synchronized locks all methods in one object<br><\/strong>These are both methods of the same object, and both are marked as synchronized. If even one method is being executed, all other synchronized methods of the same object become inaccessible to other threads<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Main {\n    public static void main(String &#91;] args) {\n        SharedClass sharedObject = new SharedClass();\n \n        Thread thread1 = new Thread(() -> {\n            while (true) {\n                sharedObject.increment();\n            }\n        });\n \n        Thread thread2 = new Thread(() -> {\n            while (true) {\n                sharedObject.decrement();\n            }\n        });\n \n        thread1.start();\n        thread2.start();\n    }\n \n    static class SharedClass {\n        private int counter = 0;\n \n        public synchronized void increment() {\n            this.counter++;\n        }\n \n        public synchronized void decrement() {\n            this.counter--;\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>synchronized lock different object<br><\/strong>thread could execute both critical sections at same time.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Main {\n    public static void main(String &#91;] args) {\n        SharedClass sharedObject = new SharedClass();\n \n        Thread thread1 = new Thread(() -> {\n            while (true) {\n                sharedObject.incrementCounter1();\n            }\n        });\n \n        Thread thread2 = new Thread(() -> {\n            while (true) {\n                sharedObject.incrementCounter2();\n            }\n        });\n \n        thread1.start();\n        thread2.start();\n    }\n \n    static class SharedClass {\n        private int counter1 = 0;\n        private int counter2 = 0;\n \n        private Object lock1 = new Object();\n        private Object lock2 = new Object();\n \n        public void incrementCounter1() {\n            synchronized (lock1) {\n                this.counter1++;\n            }\n        }\n \n        public void incrementCounter2() {\n            synchronized (lock2) {\n                this.counter2++;\n            }\n        }\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>synchronized locks all methods in one objectThese are both methods of the same object, and both are marked as synchronized. If even one method is being executed, all other synchronized methods of the same object become inaccessible to other threads synchronized lock different objectthread could execute both critical sections at same time.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-604","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/604","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=604"}],"version-history":[{"count":2,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/604\/revisions"}],"predecessor-version":[{"id":606,"href":"http:\/\/emacslisp.com\/index.php?rest_route=\/wp\/v2\/posts\/604\/revisions\/606"}],"wp:attachment":[{"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=604"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/emacslisp.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}