You are on page 1of 4

7/14/13

Synchronization and lock question (SCJP forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author

Synchronization and lock question


posted 10/26/2012 5:01:45 PM

David Samer Ranch Hand Joined: Feb 08, 2012 Posts: 44


I like...

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.

:/ does anyone feel like writing a code example , please?.

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

print

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

Synchronization and lock question (SCJP forum at JavaRanch)


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 .

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

posted 10/26/2012 7:04:25 PM

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

s y n c h r o n i z e ( a ) { s y n c h r o n i z e d ( b ) { / / . . . . d os o m ew o r kh e r e b . e a t ( ) ; / / s u p p o s eA n i m a l ' se a t ( )i sm a r k e da ss y n c h r o n i z e d . } a . e a t ( ) ; } / / T h el o c k so naa n dba r er e l e a s e dn o w . } }

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 .

c l a s sD e m oi m p l e m e n t sR u n n a b l e { 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 ) { f o r( i n ti = 0 ; i < 2 0 ; i + + ) { S y s t e m . o u t . p r i n t l n ( T h r e a d . c u r r e n t T h r e a d ( ) . g e t N a m e ( ) +"p r i n t s"+i ) ; } } } p u b l i cv o i dr u n ( ) { d o S t u f f ( ) ; } p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ) { D e m od e m o=n e wD e m o ( ) ; T h r e a dt=n e wT h r e a d ( d e m o ) ; t i m e .

/ / 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 . } }

David Samer Ranch Hand Joined: Feb 08, 2012 Posts: 44


I like...

posted 10/26/2012 10:26:39 PM

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

print

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 .

Synchronization and lock question (SCJP forum at JavaRanch)


N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped

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

s y n c h r o n i z e ( a ) { s y n c h r o n i z e d ( b ) { / / . . . . d os o m ew o r kh e r e b . e a t ( ) ; / / s u p p o s eA n i m a l ' se a t ( )i sm a r k e da ss y n c h r o n i z e d . } a . e a t ( ) ; } / / T h el o c k so naa n dba r er e l e a s e dn o w . } }

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

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 .

c l a s sD e m oi m p l e m e n t sR u n n a b l e { 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 ) { f o r( i n ti = 0 ; i < 2 0 ; i + + ) { S y s t e m . o u t . p r i n t l n ( T h r e a d . c u r r e n t T h r e a d ( ) . g e t N a m e ( ) +"p r i n t s"+i ) ; } } } p u b l i cv o i dr u n ( ) { d o S t u f f ( ) ; } p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ) { D e m od e m o=n e wD e m o ( ) ; T h r e a dt=n e wT h r e a d ( d e m o ) ; t i m e .

/ / 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

posted 10/27/2012 12:09:34 AM

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?

David Samer Ranch Hand Joined: Feb 08, 2012 Posts: 44


I like...

posted 10/27/2012 3:22:21 AM


Himai Minh wrote:

Hi, David, 1. To my knowlege, once the synchronized(b) block finishes, the lock of b is released.

We agree then , confirmed My assumptions. Thanks.

Himai Minh wrote:

https://www.coderanch.com/t/596229/java-programmer-SCJP/certification/Synchronization-lock

3/4

7/14/13

Synchronization and lock question (SCJP forum at JavaRanch)

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?

It does, clear explanation. I do appreciate it , thanks.

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

subject: Synchronization and lock question

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 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

https://www.coderanch.com/t/596229/java-programmer-SCJP/certification/Synchronization-lock

4/4

You might also like