• 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

Exercise 9-2 K&B Page 710

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think this is the solution for the Question on pg 710.But when I run this program for I get different output.I mean the second and Third Thread gets interleaved...Why does this happen? since I have syncronized the run method.All the 3 threads should complete fully at once?Please Guide what is wrong?
Thanks in advance
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Darshan

I think that because you have three instances of Thread, synchronizing the run() method won't stop the Threads interleaving, because they are not synchronized on something that they all access.

You need to synchronize on a object shared between them to stop the interleaving behaviour.

Regards,

Paul
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You can think of



like this:



and in this case for each thread, the "this" is different, and hence the non-synchronized behavior..

Regards,
Vishwa
 
Darshan Kapadia
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank Guys for the replies So can anyone suggest the changes needed to make the program give the correct output as asked in the question...
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dude....havent read the question....but from your code example i can say the following will give the desired output:--



public class stringTest implements Runnable{
private StringBuffer fer;
public synchronized void run()
{
{
for(int i=0;i<15;i++)
{
System.out.println(Thread.currentThread().getName()+"Letter = "+fer);
}
char f=fer.charAt(0);
char g=(char)(f+1);
fer.setCharAt(0,g);
}

}
public stringTest(StringBuffer der)
{
fer=der;
}

public static void main (String args[])
{
StringBuffer de=new StringBuffer("a");
stringTest s=new stringTest(de);
Thread t1 = new Thread(s);
Thread t2 = new Thread(s);
Thread t3 = new Thread(s);
t1.setName("First");
t2.setName("Second");
t3.setName("Third");
t1.start();
t2.start();
t3.start();
}
}


Here we have only one stringTest object and 3 threads accessing it.
So now the synchronization comes in play.
 
Darshan Kapadia
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got the solution

 
Rajat Asani
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Darshan,
I just read the question, I believe the code you have given has following flaws:

It is not having 3 threads working on same object, it has 3 threads working on 3 different objects.
It is not synchronizing a block of code, it is synchronizing the whole class which is actually done in case of static methods.

Solution:-
Have 3 threads working on same object.
Synchronize the object and not the class in case of non static methods.

Please refer the code i have posted above.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic