tag:blogger.com,1999:blog-8712770457197348465.post8295276435223296918..comments2024-03-28T02:47:19.159-07:00Comments on Javarevisited: What is ReentrantLock in Java? Difference between synchronized vs ReentrantLock with Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger15125tag:blogger.com,1999:blog-8712770457197348465.post-14280109237774329872020-03-09T07:38:06.620-07:002020-03-09T07:38:06.620-07:00Do you mean ReentrantLock interface of java.util.c...Do you mean ReentrantLock interface of java.util.concurrent or you just talking about re-entrant lock concept? In case of later, you don't need to acquire lock if you already have one needed for a method. In case of former, its object lock on Lock class, Java doc also has some useful info https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/locks/ReentrantLock.htmljavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-68050243543006879092020-03-06T20:51:12.281-08:002020-03-06T20:51:12.281-08:00What type of lock acquired by ReenterentLock ? If ...What type of lock acquired by ReenterentLock ? If it is Object lock then how we can achieve Class level lock or vice-versa .Arvindhttps://www.blogger.com/profile/01000361245081639919noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-10276568931140205842017-05-24T03:51:55.424-07:002017-05-24T03:51:55.424-07:00It should be final ReentrantLockHowto counter = ne...It should be final ReentrantLockHowto counter = new ReentrantLockHowto();<br /><br />in place of final ThreadTest counter = new ThreadTest();Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-63748036149252049852016-01-15T22:10:17.269-08:002016-01-15T22:10:17.269-08:00@Anonymous, the program shows how you can safely s...@Anonymous, the program shows how you can safely share a counter between two threads. Synchronization and thread-safety is provided by both synchronized keyword and ReentrantLock. If you see there are two methods getCount() and getCountTwo(), first one is using ReentrantLock and second one is using synchronized keyword, but both are protecting the counter correctly.<br /><br />Key is that javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-71615588574009163542016-01-15T02:19:01.957-08:002016-01-15T02:19:01.957-08:00I fail to understand the difference between reente...I fail to understand the difference between reententrantLock and synchronized lock based on the out of the sample program.What does Thread 1 or Thread 0 printing the count two times consequently mean.Thanks<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-83026200283028242592015-11-30T02:29:27.865-08:002015-11-30T02:29:27.865-08:00correct code would be ....
package reentrant;
/*...correct code would be ....<br /><br />package reentrant;<br /><br />/**<br /> *<br /> * @author avishek<br /> */<br />import java.util.concurrent.locks.ReentrantLock;<br />import java.util.logging.Level;<br />import java.util.logging.Logger;<br /><br />/**<br /> * Java program to show, how to use ReentrantLock in Java. Reentrant lock is an<br /> * alternative way of locking apart from implicit Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-63335958756709952012015-06-02T18:34:36.121-07:002015-06-02T18:34:36.121-07:00Where is class "ThreadTest" ?
I also do ...Where is class "ThreadTest" ?<br />I also do not see class "ReentrantLockHowto" being used ?<br />I am getting it right ?...can someone please explain...thanks in advance.<br />Anonymoushttps://www.blogger.com/profile/15320563670963052177noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-6526116888428300632014-05-01T11:03:00.746-07:002014-05-01T11:03:00.746-07:00Hi Javin,
i have a doubt regarding your code writt...Hi Javin,<br />i have a doubt regarding your code written below.<br /><br />Thread t1 = new Thread() {<br /><br /> @Override<br /> public void run() {<br /> while (counter.getCount() < 6) {<br /> try {<br /> Thread.sleep(100);<br /> } catch (InterruptedException ex) {<br /> Neetu Palhttps://www.blogger.com/profile/04356777028498562881noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65332879830710647022014-04-02T06:19:54.347-07:002014-04-02T06:19:54.347-07:00public void run() {
try {
loc... public void run() {<br /><br /> try {<br /> lock.lock();//<-----<br /> <br /> System.out.println(Thread.currentThread().getName());<br /> System.out.println(counter++);<br /> <br /> } finally {<br /> lock.unlock();<br /> }<br /> }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-6775253888155102802013-06-01T11:16:19.340-07:002013-06-01T11:16:19.340-07:00public class RentractLockHowTo extends Thread{
p...public class RentractLockHowTo extends Thread{<br /><br /> private static final ReentrantLock lock =new ReentrantLock();<br /> int counter =0;;<br /> @Override<br /> public void run(){<br /> <br /> try{<br />lock.tryLock();<br /> System.out.println(Thread.currentThread().getName());<br /> System.out.println(counter++);<br /> <br /> }finally{<br /> //lock.unlock();<br /> }<br /> }<br /> gauravhttps://www.blogger.com/profile/02382301392533656128noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-48757472166427705592013-03-27T14:06:31.465-07:002013-03-27T14:06:31.465-07:00I ran few example with almost 50 threads kicking ...I ran few example with almost 50 threads kicking at same time (also No sleep call on thread ) using reentarant lock and found that thread were still entering in block though other thread already already acquired a lock. Problem resolved when i create the reentrant object as static final (private static final ReentrantLock rLock = new ReentrantLock()) .Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-44851734555703256352013-03-21T23:17:52.476-07:002013-03-21T23:17:52.476-07:00I guess most important difference between a synchr...I guess most important difference between a synchronized lock and a ReentrantLock is to release lock in a different code block. With ReentrantLock, you can acquire lock in one method and can release in other method, but synchronized keyword forces you to release lock in same code block, doesn't matter if you synchronized method or block.JackTheRnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-77918892113563996522013-03-08T00:56:35.583-08:002013-03-08T00:56:35.583-08:00@Saral, great comment. In context of allowing thre...@Saral, great comment. In context of allowing thread to reenter to a block of code, which is also locked by same lock, implicit locking by synchronized keyword also provides same functionality. In short, implicit lock is also re-entrant. I think, ReentrantLock should be prefer over synchronized for high performance code, you can see almost all classes on java.util.concurrent uses ReentrantLock.Javin @ BlockingQueue in Javahttp://javarevisited.blogspot.com/2012/12/blocking-queue-in-java-example-ArrayBlockingQueue-LinkedBlockingQueue.htmlnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-77855391554485548492013-03-07T09:54:26.847-08:002013-03-07T09:54:26.847-08:00Reentrant lock functionality :-
==================...Reentrant lock functionality :-<br />============================<br /><br />A reentrant lock will allow the lock holder to enter blocks of code even after it has already obtained the lock by entering other blocks of code. A non-reentrant lock would have the lock holder block on itself as it would have to release the lock it obtained from another block of code to reobtain that same lock to enter SARAL SAXENAhttps://www.blogger.com/profile/01084233786047386880noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-41842089079535138492013-03-07T09:39:07.975-08:002013-03-07T09:39:07.975-08:00Javin please edit the program I think you have mis...Javin please edit the program I think you have missed to put the synchronized keyword ..it should be ..<br /><br />//Implicit locking using synchronized keyword<br /> public int synchronized getCountTwo() {<br /> return count++;<br /> }<br /><br /><br />SARAL SAXENAhttps://www.blogger.com/profile/01084233786047386880noreply@blogger.com