wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Syncronizing variables or classes - Exercise 9-2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Syncronizing variables or classes - Exercise 9-2" Watch "Syncronizing variables or classes - Exercise 9-2" New topic
Author

Syncronizing variables or classes - Exercise 9-2

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

I noticed that on page 735 of the K&S it says the following:


Only methods (or blocks) can be synchronized, not variables or classes.


Then in exercise 9-2 (page 738), step 4 of the exercise is asking for the following


The synchronized block of code will obtain the lock on the StringBuffer object from step 3.


- Why are we syncronizing on a Stringbuffer object in exercise 9-2 when the previous few pages says that only methods and code blocks can be synchronized?
- What exactly is the difference when you syncronize on an object that has no syncronized methods compared to one that has at least one syncronized method?
- The API for stringbuffer describes it as "A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. "

None of its methods are syncronized so what makes it thread safe?


Thanks
Ash Gill
Ranch Hand

Joined: Jun 30, 2011
Posts: 71
- Why are we syncronizing on a Stringbuffer object in exercise 9-2 when the previous few pages says that only methods and code blocks can be synchronized?


there is a difference between synchronizing on an object and marking a method or a block as Synchronized. what they mean is:
you cannot say: synchronized new Animal(); or synchronized int x = 6;

whereas, whenever you mark an a method or block as synchronized, the synchronization occurs on an object (means for entering the method or block, thread needs a lock for a particular object).
Ash Gill
Ranch Hand

Joined: Jun 30, 2011
Posts: 71
What exactly is the difference when you syncronize on an object that has no syncronized methods compared to one that has at least one syncronized method?


when you enter a synchronized block or method, you automatically aquire the lock on which the the block is synchronized OR in case of methods, the this object. Now when a thread aquires the lock on an object by entering the synch block or method, then any other thread cannot enter the 'synchronized methods' of that object.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Ok i get it so when they say obtain a lock for an object they mean the object that has a synchronized method or block.
How come the thread can obtain a lock on the Stringbuffer object when it has no syncronized methods or blocks? I couldnt see any syncronized methods from the StringBuffer API shown here http://download.oracle.com/javase/6/docs/api/
Ash Gill
Ranch Hand

Joined: Jun 30, 2011
Posts: 71
Ok i get it so when they say obtain a lock for an object they mean the object that has a synchronized method or block.


no, you are again mixing the two i.e: obtaining the lock for an object & entering a sychronized method or block. when you enter a synchro method or block, you obtain the lock of the object the method or block is synchronized on. there is no requirement that the object you are aquiring the lock of, should/shouldn't have a synchronized method.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Im sorry this is a bit confusing. please explain the difference between these two examples in the context of object locking


And

I know the first example will obtain a lock on the this instance and the second will obtain a lock of the aStringBufferObject instance. But i dont really understand what the effect or the difference of teh two is.

For example, in the second example, will threads still be able to execute the code inside the synchronized block because the lock is not related to the 'this' instance?

I know that synchronizing a method or a block of code prevents multiple threads to access that block/method at the same time but what is the purpose of specifying the object to lock on and what is the difference in the way the object is specified as in teh above examples.

Thanks
Ash Gill
Ranch Hand

Joined: Jun 30, 2011
Posts: 71
hey no problem Ziggy, it is indeed a bit confusing. i will try to explain.

in the first case, any thread 1 entering the synchro block, starting at line 2, will aquire the lock on the object which is being used to access that synchro block. as a result, any other thread 2 which is started by using the same object as thread 1 and hence is using the same object as thread 1 to access the synchro block, cannot do so.

in the second case, any thread entering the synchro block, starting at line 4, will aquire the lock on the object 'new StringBuffer("A")' (this object is on the heap somewhere). as a result, any other thread t, which has been started by using an object o, which (the object o) in turn has the same object as represented by 'new StringBuffer("A")', cannot enter the synchro block. i know its confusing but the reading this in conjunction of the following example might help you understand better:
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

I think what was confusing me is what exactly was being locked. I got confused as to whether it is the object instance that was being locked or the synchronized code block. I thought that when you obtain a lock on an object no other thread can access the instance variables for that object.

Looking at your example above, it looks like the 'this' reference or a reference to any other object is just two different methods of blocking two or more concurrent thread entering the synchronised block and not the data in the object itself.

Basically if one thread has a lock on an object, no other thread can access the synchronised block unless it has a lock on the object used. The use of 'this' is just a way to refer to the current instance. So you can either use the current instance or any reference to any other object as lock for the synchronized block. right?
Ash Gill
Ranch Hand

Joined: Jun 30, 2011
Posts: 71
yes Ziggy, absolutely correct. you got it.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Thanks. Got there eventually
Ash Gill
Ranch Hand

Joined: Jun 30, 2011
Posts: 71
i was wondering if you know or not but notify, wait and notifyall are not on the exam.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

I didnt know that Ash thanks for letting me know. Is there a way i can find out what is not in the exam as i would prefer to concentrate on what is on the exam.

Thanks
 
wood burning stoves
 
subject: Syncronizing variables or classes - Exercise 9-2
 
Similar Threads
K & B - Chapter 9 Threads Exercise 9-2
K&B SelfTest Ques ,chap 9 pg 739
StringBuffer Instance Variable
K&B Chapter 9 exercise 9-2 page 738.
K&B scjp5 Exercise 9.2 - Synchronizing a block of Code (Chapter 9: Thread)