• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exercise 9-2(page 738)Sun certified programmer for Java6"Study Guide"

 
Manos Kolias
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This exerscise is to attempt synchronizing a block of code.
The steps we should take are:
1.Create a class and extend the Thread class. //ok so far.
2.Override the run() method of Thread. ect... //ok again.
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. //HOW CAN WE DO THIS???

Does anyone can help me on this topic?
Thanks anyway.
Manos.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manos Kolias wrote:This exerscise is to attempt synchronizing a block of code.
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. //HOW CAN WE DO THIS???


 
Manos Kolias
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you .
 
Manos Kolias
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again.
I can not understand this exercise !!
I didn't find a method that can increment a letter in the StringBuffer class.
Can you please help me with some details ? All the methods try to append or insert something .
I m in great confusion and that confusion isn't about synchronizing a block of code rather than messing up with StringBuffer class!!

public class Test extends Thread{
private StringBuffer letter ;
public Test(){
letter = null;
}
public Test(StringBuffer sb){
letter = sb;
}
public StringBuffer getLetter(){
return letter ;
}
public void run()
{
for(int i=0;i<100;i++)
{synchronized(this){System.out.println(letter );}

}

}

}
Here is some of my thoughts.
I m expecting for your help .Thanks in advance.
Manos.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you are talking about the 9-2 exercise in the book in which we have to print AAA....BBBB...CCCCc

s.charAt(0) gives you the character at idnex position 0...you can incremeent it using ++...after incrementing it you can use s.setCharAt(0,char c)......

i hope this clears your doubt....try it yourself..else i will send you my code
 
Manos Kolias
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm ok .Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic