wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes synchronizing a block of code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronizing a block of code" Watch "synchronizing a block of code" New topic
Author

synchronizing a block of code

Suresh KumarPandey
Ranch Hand

Joined: Sep 08, 2012
Posts: 46
package com.ocjp.thread;
import java.lang.Thread;
import java.lang.*;
public class MyThread extends Thread {
StringBuffer str;
int counter=0;
public void run(){

synchronized(this){
System.out.println(counter);
for(int i=1;i<=2;i++){

System.out.println((char)(str.charAt(0)+counter));
}
++counter;
}
}

public MyThread(StringBuffer sb){
str=sb;
}


public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer sb=new StringBuffer("A");
MyThread one=new MyThread(sb);
MyThread two=new MyThread(sb);
MyThread three=new MyThread(sb);

one.start();
two.start();
three.start();
}

}

Why this code is not printing 2 B's and 2 C's after 2A's.It is printing 6 A's.

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

When you post code, please UseCodeTags (<- Click that link).

1) Why do you think the code should print As, Bs, and Cs?

2) These threads are not synchronized with each other. There are three different objects, that means three different locks, and no synchronization. But the lack of synchronization is not the problem with the letters not showing up as expected (hint: the synchronization and output are both caused by the same thing, though).


Steve
Suresh KumarPandey
Ranch Hand

Joined: Sep 08, 2012
Posts: 46
Hi Steve
Thanks for the reply


If we replace synchronized(this)
with synchronized(str)
,the str should get synchronized and but still it's not printing the desired result

please help
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1013
    
  15
How about you dry run the program on a piece of paper substituting the values of the variables in the process.

A second hint --> Synchronization and synchronization related things aren't the root cause of your problem.
A third hint --> Dry run just the for loop.

See if it helps.

Chan.
Suresh KumarPandey
Ranch Hand

Joined: Sep 08, 2012
Posts: 46
Hi chan and steve ,
Thanks for the help
every thread initialized with its own copy of local variable,so when thread were running the counter value was 0.
so i made it static and now i got the desired result.
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1013
    
  15
so i made it static and now i got the desired result.


How about you execute your changed code many times, say a 20 times or something. Are you still getting the desired output every time?

Ok, so I will also edit my earlier response.

A second hint --> Synchronization and synchronization related things aren't the root cause of your problem.


Yes, synchronization may also need some changes there.

Chan.
Suresh KumarPandey
Ranch Hand

Joined: Sep 08, 2012
Posts: 46
Hii
Chan i would like to understand
synchronized(this),i don't get it sometimes
can you help me getting it
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1013
    
  15
My bad! I didn't notice that in your previous post you had also changed synchronized(this) to synchronized(str). I thought you had just declared counter as static and hence I said you might still not get the result you want if you execute your code more than once. Ok, well done. You've solved it yourself.

Suresh KumarPandey wrote:Hii
Chan i would like to understand
synchronized(this),i don't get it sometimes
can you help me getting it


Henry has already responded to this question in your other thread.

Just to explain with respect to your current post--> 'this', as you must already be aware of, refers to the current object, i.e the object that has invoked the method where you see 'this' coded. In your case, it is the run method. When you say synchronized(this) before a block, you enforce that only one thread of that particular object can execute that block at a time. This is done to protect the object data that the threads share.

Chan.



 
It is sorta covered in the JavaRanch Style Guide.
 
subject: synchronizing a block of code