• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt on Synchronization

 
Ranch Hand
Posts: 180
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ashok Pradhan
Ranch Hand
Posts: 180
Netbeans IDE Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
synchronized (this.stringBuffer)

Can you tell what this does??
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

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 ]

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the code i write and it works fine as mentioned in the exercise .


reply
    Bookmark Topic Watch Topic
  • New Topic