• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads: synchronizing a block of code (K&B 1.5)

 
Mea Cook
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I have some problems with te exercise 9-2, Chapter 9, p. 710-711

The result must be 100 As, 100 Bs, 100 Cs

In the book are follows steps described:
1. create a class and extend the Tread class (OK, no problem)
2. override te run of Thread. This is were te synch block will go (OK)
3. create a constructor tat accept StringBuffer (OK)
4 lock StringBuffer object (OK)
5 within block output StringBuffer 100 times and then increment te letter in the StringBuffer???
6. in main() create StringBuffer object using letter A, create 3 instances and start 3 of them (OK)

Thank you very much



 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
5. within block output StringBuffer 100 times and then increment te letter in the StringBuffer???

Basically what this line means is that you have to print the value in the StringBuffer 100 times increment the letter in the StringBuffer. Now I'm sure you are confused on how to increment the letter in the StringBuffer. Checkout the charAt and setCharAt methods of StringBuffer class...
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mea Cook wrote:Hello all,
5 within block output StringBuffer 100 times and then increment te letter in the StringBuffer???

Get a letter from StringBuffer object using charAt(index) method, increment it then save it back with setCharAt(index, char).
 
Mea Cook
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you,

but I am still by this exercise...

I use in void run() for loop (0 to 99) and print 100 As, how can I use charAt(pos). I thought, I have at the beginning only 1 A in a StringBuffer-Object...And the value of StringBuffer doesnt change during 100 iterations...
And after 100 iterations I must skip to B. Must I do another loop do... while? And how can I skip? if (s.charAt(0) eq 'A')??? But this is not OK if I whant to skip from 'B' to 'C'


Thank you in advance
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing the value from 'A' to 'B' and 'B' to 'C' and so on shouldn't be that hard.
if (s.charAt(0) eq 'A')

You don't need any checks. You need to use the charAt method with setCharAt. Read what Ireneusz wrote again...
 
Mea Cook
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how can I increment a letter please??? s.charAt(0) + 1??? It doesn't work
Sorry but I am still not on the road
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mea Cook wrote:And how can I increment a letter please??? s.charAt(0) + 1??? It doesn't work

What exactly did you write?? Did you check what is the return type of charAt method?? If you know what is the return type, it shouldn't be hard to understand how to increment the value. And please can you not use emoticons ( ) everywhere...
 
Mea Cook
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be it is easy, but I don't know how to increment from A to B... and so on using charAt
Yes, I know the type of the return value. But it isn't my problem.

 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mea please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it.

Also, the button is used to suggest to the javaranch staff that your problem is solved. Don't use it till your problem is solved.

The charAt method returns a char value. You can increment it using ++ operator. Try this
 
Mea Cook
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I edited it.

Now I have anothe Problem: NullPointerException. Ist here a problem with synchronisation?

 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I have anothe Problem: NullPointerException. Ist here a problem with synchronisation?

There is no problem with synchronization. For t2 and t3, you didn't pass the StringBuffer object (localString), so it will be null...
 
Mea Cook
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit, I changed the code. Now I get output. But only A's...

 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code has two problems.

1. You need to move the code which modifies the StringBuffer into the synchronized block (otherwise there's not much point in synchronization).
2. The line c = c++; won't work because the value of c won't be changed due to this statement as you are using a postfix increment operator (read a tutorial on this). Only using c++; would work...
 
Mea Cook
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! It works!!!

I have quation about:

why setChar(0, 'A' + 1) doesn't work and

why 3 steps with a temporal variable c are need:

char c;
c = s.charAt(0);
c++;
s.setChar(0, c);

Must I open a new topic?

Best regards
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why setChar(0, 'A' + 1) doesn't work

Because when you apply the plus (+) operator on char (or byte or short), the result is an int. So if you use any explicit type cast, it will work
why 3 steps with a temporal variable c are need

Its not needed, you can directly do it as
But this syntax looks a little complicated so I used a variable...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Mea Cook! Did you get the correct output as 100 As, 100 Bs, 100 Cs. I find out some logical error in your coding.

1) You used c = c++. But I think, you should use c = ++c; or else just c++ or ++c.
2) And you have to synchronize the code as well in your synchronized code
.

I think, it's correct, If I wrong, Please correct it.........
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use :
 
Mea Cook
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all!!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic