Help coderanch get a
new server
by contributing to the fundraiser
  • 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

synchronized block

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when i run the following code , even after removing the 'synchronized(this)' keyword i get the same output, I want to print without synchronization output like "ABABABBABCBBCCC..." .any suggestions.

************************************************************************
public class Synchroniz extends Thread {


StringBuffer sb;

public Synchroniz(StringBuffer sb) {
this.sb=sb;
}
public void run() {
synchronized(this)
{
for(int i=0;i<=5;i++){
System.out.print(this.sb);
sb.append(this.sb);
}
}
}

public static void main(String args[]) {
Synchroniz synA=new Synchroniz(new StringBuffer("A"));
Synchroniz synB=new Synchroniz(new StringBuffer("B"));
Synchroniz synC=new Synchroniz(new StringBuffer("C"));
synA.start();
synB.start();
synC.start();
}
}

***********************************************************************
produces: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCC
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason might be that one thread is gettign enough time to complete the for loop, so it is printing the same result even after removing the synchrinized block.

If you make your loop to iterate through 1000 times then we may get the result in unpredictable way after removing the synchronized block.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeas...
Sanjit is right.

The only problem is no. of iterations are too small.
And that is the only reason.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) your synchronization is achieving nothing because you are not synchronizing on an object common to your three threads.

2) It is not guaranteed that making the loop longer will cause the As, Bs, Cs be mixed in the way you require.

3) The following may produce mixed output if the yielding of the thread is honoured by the thread scheduler:



On my system the output is:
ABCAABBCCAAAABBBBCCCCAAAAAAAABBBBBBBBCCCCCCCCAAAAAAAAAAAAAAAABBBBBBB
BBBBBBBBBCCCCCCCCCCCCCCCCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

The longer runs are because you are appending StringBuffers.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Replacing the Thread.yield() call with a Thread.sleep(0 to 4 milliseconds chosen randomly) produces:

ABCBBBBBBAABBBBBBBBCCBBBBBBBBBBBBBBBBCCCCBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
AAAACCCCCCCCAAAAAAAACCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

So you can play around a bit with these things but I do not think it makes much sense to do so.
[ February 01, 2007: Message edited by: Barry Gaunt ]
 
ahsan mir
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the yield method gives an alternative way, what i really wanted is make use of synchronization in an appropriate way/object.
 
This tiny ad will self destruct in five seconds.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic