tag:blogger.com,1999:blog-8712770457197348465.post5436916177351851087..comments2024-03-18T23:01:42.768-07:00Comments on Javarevisited: The Ultimate Guide of Synchronization in Java - Examplesjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger42125tag:blogger.com,1999:blog-8712770457197348465.post-57146889391591416142020-10-12T08:26:28.993-07:002020-10-12T08:26:28.993-07:00yes, that's correct. Object.class is only one ...yes, that's correct. Object.class is only one object so every other thread will be blocked when one thread is accessing it. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-80657250932059414892020-10-09T01:26:29.531-07:002020-10-09T01:26:29.531-07:00I have a question here. we can use syntax like syn...I have a question here. we can use syntax like synchronized(Object.class) {//do some logic here}. What does synchronized real locked? If it locked class object of 'Object'(Top class of all java class) class. then we know the .class object is singleton in whole JVM if they are load by same classloader. In another word, all any other thread wants to access .class object's method, it Kipper Linoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-23875176956561343592020-06-03T23:55:18.650-07:002020-06-03T23:55:18.650-07:00@Anonymous, the synchronization will happen if two...@Anonymous, the synchronization will happen if two threads will try to get into those two methods and lock is held by other. If you just have one thread or if method is doing nothing, you won't notice that behavior. If you want to confirm then start with two threads and put some sleep on both method. Alternative, just put a break-point on each method and debug, you will know the truth. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-87243464032344713092020-06-03T06:20:37.339-07:002020-06-03T06:20:37.339-07:00Question. I have two classes with different metho...Question. I have two classes with different methods using a synchronized blocks that use the same object for locking. It doesn't appear that one object holding the lock prevents the other objects sync block from running. Is this expected behavior?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-72047999905483156692019-11-26T11:53:45.925-08:002019-11-26T11:53:45.925-08:00i need the exact definition of synchronization i need the exact definition of synchronization Anonymoushttps://www.blogger.com/profile/02712070440354398236noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-43199071570585814252019-11-21T10:05:51.506-08:002019-11-21T10:05:51.506-08:00bhai sahab saral bhasa me dalo yr ye to kuch smj m...bhai sahab saral bhasa me dalo yr ye to kuch smj me ni aaya humareAnonymoushttps://www.blogger.com/profile/02712070440354398236noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-57365952575833536142019-10-27T11:04:57.122-07:002019-10-27T11:04:57.122-07:00we have local variables to resolve thread safety i...we have local variables to resolve thread safety issues ,never use instance variables.why synchronised is introduced when we have local variables .At code Design level itself we can eliminate thread related problems .Whats difference between local variable vs synchronised vs instance variables? Anonymoushttps://www.blogger.com/profile/09447489970987821606noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-47530072189703243052018-10-13T09:35:38.071-07:002018-10-13T09:35:38.071-07:00Can u please help me in getting synopsis for synch...Can u please help me in getting synopsis for synchronization in java for mini project Anonymoushttps://www.blogger.com/profile/14759012517443755196noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-58517026832480203822018-07-06T19:20:41.948-07:002018-07-06T19:20:41.948-07:00hi,javin i have two problem about your article ...hi,javin i have two problem about your article :<br /><br />first :volatile can't promise atom action for example i++ instruction.<br /><br />second:sychornized keyword if can prevent reordering of coding ,then in double check lock volatile whether can not exists Anonymoushttps://www.blogger.com/profile/03655278233936446377noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-9742757836474739762017-09-25T05:42:58.648-07:002017-09-25T05:42:58.648-07:00this sould not compile in java 9 becausec '_&#...this sould not compile in java 9 becausec '_'character resereved for <br />for lambda expresssionAnonymoushttps://www.blogger.com/profile/12613277884237351017noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-52645056112772666432017-05-04T07:38:33.026-07:002017-05-04T07:38:33.026-07:00Thank you Arun, glad you find my tutorials useful....Thank you Arun, glad you find my tutorials useful. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-48815526871129988362017-05-04T07:25:49.602-07:002017-05-04T07:25:49.602-07:00Your tutorial is very good. ThanksYour tutorial is very good. ThanksArun Singhhttps://www.blogger.com/profile/00145955392915063714noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-34362602223970473002015-05-20T02:47:24.283-07:002015-05-20T02:47:24.283-07:00Under the section "Important points of synchr...Under the section "Important points of synchronized keyword in Java" #19 'Do not synchronize on non final field on synchronized block in Java' example, isn't the String class object final by default.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-54248162291790132002014-07-05T08:18:06.911-07:002014-07-05T08:18:06.911-07:00For anyone interested in what the JVM does:
http:/...For anyone interested in what the JVM does:<br />http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-3.html#jvms-3.14<br />Unknownhttps://www.blogger.com/profile/06736508830271559281noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-88378860824020613642014-03-27T10:03:56.213-07:002014-03-27T10:03:56.213-07:00@Radhika
Yes we can use local variable or non fina...@Radhika<br />Yes we can use local variable or non final variable for synchronization but they will or may lead you to the problems.<br />1. Since new local variables gets created for every call to the method so there is no point in synchronizing on local variable because another thread running in same method will be having different object in same variable.<br /><br />2. Similarly for non final Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-35246900110597589642014-02-21T23:05:33.354-08:002014-02-21T23:05:33.354-08:00check if BluetoothBrowser.main(null); is blocking ...check if BluetoothBrowser.main(null); is blocking call or not, since event handling and graphics drawing is done by Event dispatcher thread, it won't draw anything until it return from your action listener code.javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-48395079443954909362014-02-21T20:43:40.712-08:002014-02-21T20:43:40.712-08:00Hi...i have some issue with synchronization....ple...Hi...i have some issue with synchronization....please help me.<br />My application is a swing application. I have a button. The button action code is below<br /><br />LabelStatus.setText("searching");<br />BluetoothBrowser.main(null);<br /><br />BluetoothBrowser class contain synchronized block. Now my problem is when i am click on the button it doesn't display the text on the Anonymoushttps://www.blogger.com/profile/05453968619869327764noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-46519727250260402352013-08-15T21:03:49.196-07:002013-08-15T21:03:49.196-07:00In one of Interview, I was asked couple of questio...In one of Interview, I was asked couple of questions from Java Synchronization, It would be good if you can answer this here:<br /><br />Can you synchronize on local variable or non final variable in Java? If you do, what problem will you face?<br /><br />Why Object Lock should be private, if used to lock a critical section?<br /><br />My answer to the first question was YES. Java SynchronizationRadhikanoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-27506373202343594952013-03-01T18:59:12.524-08:002013-03-01T18:59:12.524-08:00@Anonymous
- I don't see synchronized keyword...@Anonymous<br /><br />- I don't see synchronized keyword on write() method.<br />- you are using two different thread and two different object, which means there is effectively no locking. In your current code, try making write() method static synchronized.<br /><br />Javin @ Java Classloder Workinghttp://javarevisited.blogspot.com/2012/12/how-classloader-works-in-java.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-19626455522832770892013-03-01T11:03:05.621-08:002013-03-01T11:03:05.621-08:00public class Letters extends Thread {
private Stri...public class Letters extends Thread {<br />private String name;<br />public Letters(String name) { this.name = name; }<br />public void write() {<br />System.out.print(name);<br />System.out.print(name);<br />}<br />public static void main(String[] args) {<br />new Letters("X").start();<br />new Letters("Y").start();<br />} <br /><br /><br />public void run() { synchronized(Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-35810624936537871782012-12-30T18:01:03.769-08:002012-12-30T18:01:03.769-08:00I'm not understanding why the double checked l...I'm not understanding why the double checked locking code would throw a null pointer exception. The code is identical to the ones here http://javarevisited.blogspot.sg/2011/06/volatile-keyword-java-example-tutorial.htmlKarenhttps://www.blogger.com/profile/06071738114100470318noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-48256841302530023082012-11-29T07:55:59.195-08:002012-11-29T07:55:59.195-08:00If we used synchronized in singleton class it will...If we used synchronized in singleton class it will allow to access the object for only one user. if 100 users wants to access the same object then it has to take some seconds of time to release the lock for every user like that how much time it takes for 100th user? In this scenario, how can we handle ?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-80741417582274847922012-09-23T10:30:42.453-07:002012-09-23T10:30:42.453-07:00@Anonymous
The lock release occurs even if the r...@Anonymous<br /><br /> The lock release occurs even if the return was caused by an uncaught exception.<br /><br />Source : http://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.htmlAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-62414005601026764372012-06-24T01:00:23.945-07:002012-06-24T01:00:23.945-07:00Hello there :) me again...
There's a small gap...Hello there :) me again...<br />There's a small gap here:<br />"Lock is released .... or due to any Error or Exception"<br /><br />The thread does NOT release the lock when an exception is thrown, and the program stalls indefinitely.hence a kind of deadlock appears, since the second thread cannot acquire the Lock that thread 1 acquired, and did not released it;<br />I coded a small Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-92211440705630176882012-05-29T18:48:06.051-07:002012-05-29T18:48:06.051-07:00Interesting article. The heading promises that it...Interesting article. The heading promises that it will tell us how synchronization works. Instead it only tells us how to use synchronization. Disappointed...Anonymousnoreply@blogger.com