Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

thread q

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 B {
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"; //1
}}}


acc to the answer given output is ABC. explanation given is that since lock on object sa is only released by main after executing 1.
i have a query . Arent 2 objects (sa in B and sa in A) different and hence locks are different and hence output shd be XYZ. someone kindly reply
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are passing is actually a string array reference. Both the sa's point to the same thing and that's why since the main thread had already acquired the lock for 'sa', the new thread could not execute the run() method.

regards,
vijay.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Steven,

it's this thing about references... if somewhere, an object copy (eg. by sa.clone()) was made, then there were two objects.

So you just have two references to sa.

Nice question, I didn't notice the effect of the synchronized block in the first instance...

Regards
 
steven gerrard
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if sa hadnt been a string but a simple Object , then i guess output be different cause Strings are immutable hence only 1 object created .
someone pls confirm
 
Tilo Hemp
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually, sa is a "simple" object, because it is an array of strings...
if it would have only been a string, then everything would be a little trickier...

I just tried, and it would be possible by replacing sa by a String s.

It is interesting that during the synchronized block, the reference to the object we are synchronizing on can be changed:

String s = "original object";
synchronized(s){
Thread t1 = new A(s); t1.start();
s = "changed object";
}

(output is "original object")
[ February 11, 2006: Message edited by: Tilo Hemp ]
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tilo Hemp:

I just tried, and it would be possible by replacing sa by a String s.

It is interesting that during the synchronized block, the reference to the object we are synchronizing on can be changed:

String s = "original object";
synchronized(s){
Thread t1 = new A(s); t1.start();
s = "changed object";
}

(output is "original object")



Be careful doing this... Both threads are trying to grab the same object lock because the reference "s" was the same. If a third thread, tries to synchronized on "s" after it has been changed, it would be synchronizing on a different object, and hence, would run in parallel.

BTW, this is a common problem... remember, synchronization is on objects, not references.

Henry
 
Bring out your dead! Or a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic