This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on Synchronization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt on Synchronization" Watch "Doubt on Synchronization" New topic
Author

Doubt on Synchronization

Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

Can someone explain me the Exercise 9-2 page 710 on K&B book from step 3,4,5,6.
How to increase the character within the string buffer.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Originally posted by Ashok Pradhan:
Can someone explain me the Exercise 9-2 page 710 on K&B book from step 3,4,5,6.
How to increase the character within the string buffer.


You will have to post the code here. Not everyone has the SCJP 5 study guide.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Ashok Pradhan
Ranch Hand

Joined: Dec 17, 2007
Posts: 180

OK,Thanks

Exercise 9-2 K&B Book chapter Threads

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 at tempt 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 5 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.
Noam Wolf
Ranch Hand

Joined: Jan 12, 2008
Posts: 35
Hi Ashok,

This is my attempt at solving exercise 9-2 from K&B. (If anyone spots any mistakes, or a better solution please chime in).



hope that helps.


because .net guys can also write in java
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
synchronized (this.stringBuffer)

Can you tell what this does??


Thanks<br />Dinesh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37900
    
  22
Synchronized means that only one Thread can access the block.

As far as I remember: synchronized (this.stringBuffer) means that whenever a Thread enters that block, the stringBuffer object (not a good identifier, too similar to the class name) acquires a "lock" on that block and won't allow another Thread access. When the Thread has finished, the stringBuffer obejct releases the block and another Thread can apply for access.
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
are you talking of run method block.
If yes, that can also be done by using synchronized word in front of method i.e public synchronized void run(){ }.
Correct me if iam wrong..
Noam Wolf
Ranch Hand

Joined: Jan 12, 2008
Posts: 35
@Campbell - why is it not a good identifier? it is what it is. Plus using the "this" prefix gives you context. (don't forget that there is not application context since this is just sample code...)

@Dinesh - if you read the description of what needs to be done and look at the code you'll notice that it is in fact the run method. Also you are correct about making the method synchronized, but that wasn't the question:
2 Override the run() method of Thread. This is where the synchronized block of code will go.


hope that helps.
[ May 26, 2008: Message edited by: Noam Wolf ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37900
    
  22
Originally posted by Noam Wolf:
@Campbell - why is it not a good identifier?
Because the identifier for the object differs only by S/s from the name of a class.
Rajiv Chopra
Ranch Hand

Joined: Dec 19, 2008
Posts: 62
I just solved the problem.

public class ClassOne extends Thread {
private StringBuffer sb;

public ClassOne(StringBuffer sbx) {
sb = sbx;
}

public void run() {
synchronized (this.sb) {
ClassOne co1 = new ClassOne(sb);
co1.printmethod();
}
}

public void printmethod() {
System.out.println("Thread :" + sb);
for (int i = 0; i < 100; i++) {
System.out.println(sb);
}
char c = this.sb.charAt(0);
this.sb.setCharAt(0, ++c);

}

public static void main(String[] sr) {

StringBuffer sb1 = new StringBuffer("A");

ClassOne cs1 = new ClassOne(sb1);
ClassOne cs2 = new ClassOne(sb1);
ClassOne cs3 = new ClassOne(sb1);

cs1.start();
cs2.start();
cs3.start();
}
}
[ December 19, 2008: Message edited by: Rajiv Chopra ]

Rajeev Kumar
SCJP 6.0 Certified 96%
Rajiv Chopra
Ranch Hand

Joined: Dec 19, 2008
Posts: 62
Originally posted by Rajiv Chopra:
I just solved the problem. Please let me know if anything wrong!!

public class ClassOne extends Thread {
private StringBuffer sb;

public ClassOne(StringBuffer sbx) {
sb = sbx;
}

public void run() {
synchronized (this.sb) {
ClassOne co1 = new ClassOne(sb);
co1.printmethod();
}
}

public void printmethod() {
System.out.println("Thread :" + sb);
for (int i = 0; i < 100; i++) {
System.out.println(sb);
}
char c = this.sb.charAt(0);
this.sb.setCharAt(0, ++c);

}

public static void main(String[] sr) {

StringBuffer sb1 = new StringBuffer("A");

ClassOne cs1 = new ClassOne(sb1);
ClassOne cs2 = new ClassOne(sb1);
ClassOne cs3 = new ClassOne(sb1);

cs1.start();
cs2.start();
cs3.start();
}
}

[ December 19, 2008: Message edited by: Rajiv Chopra ]
Rufat Piriyev
Ranch Hand

Joined: Mar 15, 2010
Posts: 31
Hello All forum users.It's my first post in this forum.

Please sorry if my question is silly.
I can't understand some consepts of this code

If I write code like this the output isn't correct


But if I write code like this it works fine . Where is difference ?

I think in first time it's create new object each time so they don't block each other. Please sorry if you have problem to understand me.My English is so bad. And one question more. How many threads questions are in real exam . It's my weak area becouse I haven't good experience with threads
 
 
subject: Doubt on Synchronization