• 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

synchronizing a block of code

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

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).

 
Suresh KumarPandey
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 46
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hii
Chan i would like to understand
synchronized(this),i don't get it sometimes
can you help me getting it
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.



 
reply
    Bookmark Topic Watch Topic
  • New Topic