• 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

thread

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,we need to discuss on how the output for the below program is generated?

class A extends Thread
{
String[] sa;
public A(String[] sa)
{
this.sa = sa;
}
public void run()
{
synchronized (sa)
{
System.out.print(sa[0] + sa[1] + sa[2]);
}
}
}
class B09
{
private static String[] sa = new String[]{"X","Y","Z"};
public static void main (String[] args)
{
synchronized (sa)
{
Thread t1 = new A(sa);
t1.start();
sa[0] = "A"; sa[1] = "B"; sa[2] = "C";
}
}
}
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
venkat,
U should be knowing about the synchronized behaviour.
Here what happens is that since the main thread first acquires lock on the "sa" object. So the thread A waits for its turn to run,
When the main thread completes its running, the thread A is allowed to run and so it displays the changed values.

Hope its clear to u.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic