• 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

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across the questions in mock exam which is: Is class A is thread safe? My answer is as it is using instance variable x it is not thrad safe, am I right?

class A{
int X=0;
syncronized int increment()
{ int n = X;
X = n+1;
return;
}
}
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Miki

Every Object has one lock and when a method is synchronized then thread running that method acquires the lock and while that thread is running no other thread can aquire the lock as well as run that method. This class is thread safe becasue no other method is accessing the instance variable except this method. If there would have been another method accessing the instance variable and the other method is not synchronized then only the method which is synchronized would have been thread safe.

There is another scenario in which sometimes there is a call of non- synchronized method from synchronized method only. that also makes the class thread safe.

I hope this 'll help

uzma
 
uzma Akbar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Miki


This I got from the net:

The first rule to follow when making a class thread-safe through synchronizing its critical sections, therefore, is to make its fields private. Any field that you need to coordinate multithreaded access to must be private, otherwise it may be possible for other classes and objects to ignore your critical sections and access the fields directly.

I think it makes sense. What do you say?

Please read the following for your further understanding:

http://www.artima.com/designtechniques/threadsafety.html


thanks

uzma
 
Rahul Bajaj
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Uzma. It has definately helped me.
 
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

I wondered if the variable x can be accessed while another thread holds the object lock (mostly because of confusion with the volatile keyword, which is not part of the exam).

After writing a test code, the answer is "yes", the variable can be changed despite the lock.

So, wouldn't it make sense to allow class variables to be declared synchronized? Why can't they?

Any input would be nice...
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By making the class variables private, synchronizing the methods that utilize them helps ensure thread safety. Since you should do this anyways, it cuts down on the need for syncrhonized vars.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but what if one thread try to access the private variable X from synchronized method and on the same time another thread tries to access the same variable from a non synch method of the same class.

I am saying so since having a lock on an object do not allow any other thread to access any synch methods of the class at the same time but it does allow other threads accessing non synch method of the class.

Is my Program still thread safe???
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic