aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads: synchronizing a block of code (K&B 1.5) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads: synchronizing a block of code (K&B 1.5)" Watch "Threads: synchronizing a block of code (K&B 1.5)" New topic
Author

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

Mea Cook
Greenhorn

Joined: Jan 31, 2010
Posts: 11
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

Joined: Aug 03, 2008
Posts: 9317
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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

Joined: Jan 31, 2010
Posts: 11
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

Joined: Aug 03, 2008
Posts: 9317
    
  17

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

Joined: Jan 31, 2010
Posts: 11
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

Joined: Aug 03, 2008
Posts: 9317
    
  17

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

Joined: Jan 31, 2010
Posts: 11
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

Joined: Aug 03, 2008
Posts: 9317
    
  17

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

Joined: Jan 31, 2010
Posts: 11
I edited it.

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

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9317
    
  17

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

Joined: Jan 31, 2010
Posts: 11
Ankit, I changed the code. Now I get output. But only A's...

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9317
    
  17

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

Joined: Jan 31, 2010
Posts: 11
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

Joined: Aug 03, 2008
Posts: 9317
    
  17

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

Joined: Nov 04, 2009
Posts: 2066

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.........


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

I use :
Mea Cook
Greenhorn

Joined: Jan 31, 2010
Posts: 11
Thank you all!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads: synchronizing a block of code (K&B 1.5)