wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K & B - Chapter 9 Threads 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 "K & B - Chapter 9 Threads Exercise 9-2" Watch "K & B - Chapter 9 Threads Exercise 9-2" New topic
Author

K & B - Chapter 9 Threads Exercise 9-2

Saruwatari Tadayoshi
Greenhorn

Joined: Jun 22, 2010
Posts: 6
Hello,

I was just wondering if my answer looks correct for the following exercise 9-2 thank you.

Synchronizing a Block of Code
In this exercise we will attempt to synchronize a block of code. Within that block of
code we will get the lock on an object, so that other threads cannot modify it while
the block of code is executing. We will be creating three threads that will all attempt
to manipulate the same object. Each thread will output a single letter 100 times, and
then increment that letter by one. The object we will be using is StringBuffer.
We could synchronize on a String object, but strings cannot be modified once
they are created, so we would not be able to increment the letter without generating
a new String object. The final output should have 100 As, 100 Bs, and 100 Cs all in
unbroken lines.

1. Create a class and extend the Thread class.
2. Override the run() method of Thread. This is where the synchronized
block of code will go.
3. For our three thread objects to share the same object, we will need to create
a constructor that accepts a StringBuffer object in the argument.
4. The synchronized block of code will obtain a lock on the StringBuffer
object from step 3.
5. Within the block, output the StringBuffer 100 times and then increment
the letter in the StringBuffer. You can check Chapter 6 for StringBuffer
methods that will help with this.
6. Finally, in the main() method, create a single StringBuffer object using the
letter A, then create three instances of our class and start all three of them.


My Answer:

Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

That looks about right. I skimmed through the requirement and code so I ll let someone correct me in a later post if I am wrong.

Good job


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

It's OK, but, what about the output? It seems to be like, there may be 100 time C first, and then 100 times B, and then 100 time A. And other combination is also there! Is it OK?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Abimaran Kugathasan wrote:It's OK, but, what about the output? It seems to be like, there may be 100 time C first, and then 100 times B, and then 100 time A.

I don't think the output can be in the order you are saying. There will be 100As then 100Bs then 100Cs. The thread execution order might change so thread TWO might print 100As then thread THREE might print 100Bs and Thread ONE 100Cs...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Saruwatari Tadayoshi
Greenhorn

Joined: Jun 22, 2010
Posts: 6
Hello,

Thank you for the replies. It was my understanding that this is what synchronized is meant to prevent from happening? What makes you think that the output might be not in order?

I changed my answer so it loops just 20 times instead of 100 that way I have less output to post here. I have run the code 10 times and I always get the following output shown below. Although I know this is no gurantee but I see no better way of testing it?

I don't see in the question where it stipulates that Thread ONE must print A's, Thread TWO must print B's and Thread THREE must print C's? The question just states that the order of the output must be A's followed by B's and then finally C's not which thread is used to output them. It does seem I missed something from the question though as it states 'The final output should have 100 As, 100 Bs, and 100 Cs all in unbroken lines.' I am printing a new line for each I guess I just need to change the println to print.


Thanks for any comments and suggestions about my answer.

Thread ONE (0) = A
Thread ONE (1) = A
Thread ONE (2) = A
Thread ONE (3) = A
Thread ONE (4) = A
Thread ONE (5) = A
Thread ONE (6) = A
Thread ONE (7) = A
Thread ONE (8) = A
Thread ONE (9) = A
Thread ONE (10) = A
Thread ONE (11) = A
Thread ONE (12) = A
Thread ONE (13) = A
Thread ONE (14) = A
Thread ONE (15) = A
Thread ONE (16) = A
Thread ONE (17) = A
Thread ONE (18) = A
Thread ONE (19) = A
Thread THREE (0) = B
Thread THREE (1) = B
Thread THREE (2) = B
Thread THREE (3) = B
Thread THREE (4) = B
Thread THREE (5) = B
Thread THREE (6) = B
Thread THREE (7) = B
Thread THREE (8) = B
Thread THREE (9) = B
Thread THREE (10) = B
Thread THREE (11) = B
Thread THREE (12) = B
Thread THREE (13) = B
Thread THREE (14) = B
Thread THREE (15) = B
Thread THREE (16) = B
Thread THREE (17) = B
Thread THREE (18) = B
Thread THREE (19) = B
Thread TWO (0) = C
Thread TWO (1) = C
Thread TWO (2) = C
Thread TWO (3) = C
Thread TWO (4) = C
Thread TWO (5) = C
Thread TWO (6) = C
Thread TWO (7) = C
Thread TWO (8) = C
Thread TWO (9) = C
Thread TWO (10) = C
Thread TWO (11) = C
Thread TWO (12) = C
Thread TWO (13) = C
Thread TWO (14) = C
Thread TWO (15) = C
Thread TWO (16) = C
Thread TWO (17) = C
Thread TWO (18) = C
Thread TWO (19) = C
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
The question also does not state which thread to start first.

So the logic is to see a thread completes printing out a single character for its execution.

If you remove the synchronized block & x loops to a high value,
single character per thread will not be guaranteed.


Regards,
Avishkar Nikale
Saruwatari Tadayoshi
Greenhorn

Joined: Jun 22, 2010
Posts: 6
Avishkar Nikale wrote:The question also does not state which thread to start first.

So the logic is to see a thread completes printing out a single character for its execution.

If you remove the synchronized block & x loops to a high value,
single character per thread will not be guaranteed.



This is true if I was to remove the synchronized block then the output is not guranteed but the question states to use a synchronized block in the answer. The question is testing knowledge of the synchronized statement and specifies to use it. I have tested the code without it and sure enough the output order is random. So can I take it from this that my answer is correct? Other than the bit about not printing the letters on unbroken lines which is a simple fix.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Ankit Garg wrote:
I don't think the output can be in the order you are saying. There will be 100As then 100Bs then 100Cs. The thread execution order might change so thread TWO might print 100As then thread THREE might print 100Bs and Thread ONE 100Cs...


Abimaran Kugathasan wrote:........, And other combination is also there! Is it OK?


I mentioned about that already. Thanks anyway...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Abimaran Kugathasan wrote:It seems to be like, there may be 100 time C first, and then 100 times B, and then 100 time A.

Abimaran Kugathasan wrote:
Ankit Garg wrote:
There will be 100As then 100Bs then 100Cs. The thread execution order might change so thread TWO might print 100As then thread THREE might print 100Bs and Thread ONE 100Cs...

I mentioned about that already.

You said that first 100 Cs might be displayed, then 100 Bs and then 100 As which cannot happen...
 
Consider Paul's rocket mass heater.
 
subject: K & B - Chapter 9 Threads Exercise 9-2
 
Similar Threads
Doubt on Synchronization
Threads: synchronizing a block of code (K&B 1.5)
K&B scjp5 Exercise 9.2 - Synchronizing a block of Code (Chapter 9: Thread)
Solution for Exercise 9.2 (K&B)
KATHY SCJP 1.6 : THREADS, exercise 9-2. Is my code fine?