Professional Documents
Culture Documents
Java FAQ
Recent Topics
Register / Login
Author
Greetings everyone
Once again I am having some trouble understanding some concepts. From Kathy's and Bert's book (SJCP 6) , Chapter 9 - Threads- , pages 735, 736 and 737 . The get and would appreciate a practical example . (What a tough chapter) First one: (Page 736)
A thread can acquire more than one lock. For example, a thread can enter a synchronized method, thus acquiring a lock, and then immediately invoke a synchronized method on a different object, thus acquiring that lock as well. As the stack unwinds, locks are released again. Also, if a thread acquires a lock and then attempts to call a synchronized method on that same object, no problem. The JVM knows that this thread already has the lock for this object, so the thread is free to call other synchronized methods on the same object, using the lock the thread already has.
Second one: The way you can change a synchronized method to a synchronized code block: (Page 736 to 737 )
When you synchronize a method, the object used to invoke the method is the object whose lock must be acquired. But when you synchronize a block of code, you specify which object's lock you want to use as the lock, so you could, for example, use some third-party object as the lock for this piece of code. That gives you the ability to have more than one lock for code synchronization within a single object. Or you can synchronize on the current instance (this) as in the code above. Since that's the same instance that synchronized methods lock on, it means that you could always replace a synchronized method with a non-synchronized method containing a synchronized block. In other words, this:
view plain
c opy to c lipboard
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 .
p u b l i cs y n c h r o n i z e dv o i dd o S t u f f ( ){ S y s t e m . o u t . p r i n t l n ( " s y n c h r o n i z e d " ) ; }
is equivalent to this:
https://www.coderanch.com/t/596229/java-programmer-SCJP/certification/Synchronization-lock
1/4
7/14/13
view plain c opy to c lipboard
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 .
p u b l i cv o i dd o S t u f f ( ){ s y n c h r o n i z e d ( t h i s ){ S y s t e m . o u t . p r i n t l n ( " s y n c h r o n i z e d " ) ; } }
I have a feeling exam will be playing with this somehow way often than I think. Basically , does it mean I can change synchronized method to just a synchronized block of code , in order to avoid (if necessary) get whole method synchronized. Can we do this for any synchronized method? the lock in this example) . Anyone can provide a better practical example? I do understand lock as hmm kind of "state" perhaps? Thank you in advance
Also, in that example, synchronized(this) ...looks like you are synchronizing "this" object rather than code block (and no idea how to determine which thread objec
Himai Minh Ranch Hand Joined: Jul 29, 2012 Posts: 337
First answer:
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 .
c l a s sM y C l a s s { A n i m a la=n e wA n i m a l ( ) ; A n i m a lb=n e wA n i m a l ( ) ; v o i dm e t h o d 1 ( ) {
/ / s u p p o s et h e r ei sa nA n i m a lc l a s s
Second answer: Yes. Both doStuff() methods are the same. So , what is "this" refering to?
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 .
/ / B o t ht 1a n dts h a r e st h es a m eD e m od e m oi n s t a n c e .B u to n l yo n et h r e a de x e c u t e st h er u nm e t h
T h r e a d t 1 =n e wT h r e a d ( d e m o ) ; t 1 . s t a r t ( ) ; t . s t a r t ( ) ;/ / T h er u nm e t h o do fD e m oi se x e c u t e d .T h e" t h i s "i nt h ed o S t u f f ( )m e t h o di st h ed e m oo b j e c t ,w h i c hi n v o k e s t u f f ( )m e t h o d . } }
Hey, nice to see you around Himai ;) Let's go with doubts! In the first given example (thanks by the way) , which looks like :
view plain
c opy to c lipboard
https://www.coderanch.com/t/596229/java-programmer-SCJP/certification/Synchronization-lock
2/4
7/14/13
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 .
c l a s sM y C l a s s { A n i m a la=n e wA n i m a l ( ) ; A n i m a lb=n e wA n i m a l ( ) ; v o i dm e t h o d 1 ( ) {
/ / s u p p o s et h e r ei sa nA n i m a lc l a s s
b isn't released on second block finish? ( exactly in the curly brace after b.eat() in line 10 ). Why both locks from both objects, a and b, gets released once first synchronized block finish? As for the second example :
view plain
c opy to c lipboard
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 . 1 0 . 1 1 . 1 2 . 1 3 . 1 4 . 1 5 . 1 6 . 1 7 . 1 8 . 1 9 . 2 0 . 2 1 .
/ / B o t ht 1a n dts h a r e st h es a m eD e m od e m oi n s t a n c e .B u to n l yo n et h r e a de x e c u t e st h er u nm e t h
T h r e a d t 1 =n e wT h r e a d ( d e m o ) ; t 1 . s t a r t ( ) ; t . s t a r t ( ) ;/ / T h er u nm e t h o do fD e m oi se x e c u t e d .T h e" t h i s "i nt h ed o S t u f f ( )m e t h o di st h ed e m oo b j e c t ,w h i c hi n v o k e s t u f f ( )m e t h o d . } }
Clear example thanks.In this case, you are using an instance of the class itself (Demo) , in order to make it a Runnable target while using 2 threads. When t1 start method, if the other thread, t wants to start , it simply can't cause both are sharing Demo runnable instance? (So it gets t1 with lock) Assuming t1 is the first one w working.
Himai Minh Ranch Hand Joined: Jul 29, 2012 Posts: 337
Hi, David, 1. To my knowlege, once the synchronized(b) block finishes, the lock of b is released.
2. Here is a possible case: 1) t1 starts and starts executing doStuff() method of the demo instance. 2) t starts and starts executing doStuff method of the same demo. 3) Since doStuff is invoked by demo and demo is locked by t1 in step 1, t1 prints 0...20. 4) t1 releases the demo's lock. 5) Now, it is the turn for t, which has been waiting for the lock of demo since step 2. 6) Since t1 has released demo's lock, t can lock demo now and print 0.....20. Does it make sense?
Hi, David, 1. To my knowlege, once the synchronized(b) block finishes, the lock of b is released.
https://www.coderanch.com/t/596229/java-programmer-SCJP/certification/Synchronization-lock
3/4
7/14/13
2. Here is a possible case: 1) t1 starts and starts executing doStuff() method of the demo instance. 2) t starts and starts executing doStuff method of the same demo. 3) Since doStuff is invoked by demo and demo is locked by t1 in step 1, t1 prints 0...20. 4) t1 releases the demo's lock. 5) Now, it is the turn for t, which has been waiting for the lock of demo since step 2. 6) Since t1 has released demo's lock, t can lock demo now and print 0.....20.
Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Similar Threads Thread Thread Thread synchronized method Please explain Difference between synchronized method and synchrozed block
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:42:09 .
https://www.coderanch.com/t/596229/java-programmer-SCJP/certification/Synchronization-lock
4/4