• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Synchronized doubt

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

I have tried my program for this question but I am not getting my desired output, I need 100 A's ,100 B's and 100 C's ..
I am getting the output but the thing is the output is not ordered like AAABBBBBBBBBCCCCCC etc like this.
Kindly,let me know the bug in my code.

 
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please QuoteYourSources.


 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what output are you getting? How does it differ from what you want?

(And why are you setting the name of the thread after starting it?)
 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my question , its actually an scjp book exercise.

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

because my output should print 100 A ,B ,C
TO check that I am setting names for my Thread instances.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have tried my program for this question but I am not getting my desired output, I need 100 A's ,100 B's and 100 C's ..
I am getting the output but the thing is the output is not ordered like AAABBBBBBBBBCCCCCC etc like this.



This is because you aren't using any mechanism to have the processing in that order. Remember the scheduler can schedule the threads in any order.
One simple way to achieve the kind of ordered processing you want is to use the join method. You might want to explore about the method in the API docs.
Although such a processing would make threading unnecessary but I understand this is probably a learning assignment.
For the kind of output you want, I wouldn't set the Thread name after starting the Thread. I would do it before starting the thread. Do you know why?

 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No heena, I am using syncrhronized keyword to produce the output in order.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthikeyan Pandian wrote:This is my question , its actually an scjp book exercise.



Fine. We still want you to QuoteYourSources.
"Quoting the source often helps with the Copyrights. Besides, attribution is always a good thing to do".
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthikeyan Pandian wrote:No heena, I am using syncrhronized keyword to produce the output in order.



All the synchronized keyword is doing is ensuring that while one thread has locked the sb, another thread cannot lock it and hence cannot execute the following block.



So if the scheduler schedules Thread C to run before all the other threads, C will be printed 100 times and then only other threads can execute that block. It doesn't say that the Thread A should run before Thread B and that Thread B should run before Thread C.
 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



This question is taken from SCJp 6.0 by Kathy Sierra

From the chapter Threads in sub topic Synchronization and Locks.

 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@HEENA

Is the question also doesnt mean that the output should be in order right??
Because I again gone through the question deeply , its just mentioned that the output should produce 10 counts of A , B and C.

AM i right??
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthikeyan Pandian wrote:@HEENA

Is the question also doesnt mean that the output should be in order right??
Because I again gone through the question deeply , its just mentioned that the output should produce 10 counts of A , B and C.

AM i right??



How does it matter what the question is actually asking you to do? You are learning. Try to solve both the questions.

Or a third one too, if you can come up with it.
 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Heena

I got solutions to both the questions.
But i cant guess the third one.
Anyways thanks for clearing my doubt . I got some clarity in my topic.
 
Heena Agarwal
Ranch Hand
Posts: 262
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karthikeyan Pandian wrote:@Heena

I got solutions to both the questions.


That's good to know. Would you want to tell us what the solution looks like?

Karthikeyan Pandian wrote:
But i cant guess the third one.


Ok, ignore that.

Karthikeyan Pandian wrote:
Anyways thanks for clearing my doubt . I got some clarity in my topic.


You're welcome.
 
Karthikeyan Pandian
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got solutions as i described below

Series of B , C and A in first question.
And when I used the Join()
I got first A then B and finally C
 
Hey! You're stepping on my hand! Help me tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic