• 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

Doubt in thread synchronization

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

If say there are two threads T1 and T2 and both have the same instance of a class AA.

Class AA {

synchronized void method a()
{ //some code
method b(); /// line 1
}

void method b()
{
//some code
method a();
}
}

now if

1) T1 runs method a() and enters method b () through line 1 then, at that time, can T2 access method b() for the same instance of class AA.

I tried to create such a scenario but i couldn't clearly see wht is going on. I would be very kind if someone can help.

Many Thanks in advance

 
Ranch Hand
Posts: 35
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread T2 should have no problem accessing b() because it is not synchronized.

I tried some of the code below and got both threads in b() at the same time. Though it took a little playing around with the time on the sleep() method to get the Threads to be in the method at the same time.



Eventually I got the results below, which demonstrate that 't2' is in method b() while 't1' is as well. The time aspect in the results really doesn't help, but I was hoping for milliseconds.

t1 is starting b() at Thu Jun 30 18:02:11 CDT 2011
t2 is starting b() at Thu Jun 30 18:02:11 CDT 2011
t2 is leaving b() at Thu Jun 30 18:02:11 CDT 2011
t1 is leaving b() at Thu Jun 30 18:02:11 CDT 2011
 
Ash Gill
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aj, That makes complete sense. thanks a lot for the coding example.
 
Yeah, but is it art? What do you think tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic