• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

K & B - Chapter 9 Threads Exercise 9-2

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Saruwatari Tadayoshi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Saruwatari Tadayoshi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic