• 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

Synchronization

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I found this question in one of the mock exams.
class A
{
int c, d;
void assign (int x, int y)
{
c = x;
d = y;
}
}
How to enable the class A to be accessed by multiple threads?
Can anybody out there answer this question for me? What do we have to do to synchronize the whole class?
Thanks.
Kezia.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot synchronize the whole class. With this simple class the easiest way is to add the synchronized modifier to the assign method.
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kezia Matthews:
Hi All,
I found this question in one of the mock exams.
class A
{
int c, d;
void assign (int x, int y)
{
c = x;
d = y;
}
}
How to enable the class A to be accessed by multiple threads?
Can anybody out there answer this question for me? What do we have to do to synchronize the whole class?
Thanks.
Kezia.


To synchronize the whole class, use the static method of the class
Ragu
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

To synchronize the whole class, use the static method of the class



What static method? Do you mean to declare the class as a static class? If you do this can you still have synchronized methods? Is it possible to have methods that are both static and sychronized?
Sorry for these questions, but as you can tell, Threads are not something I use much.

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

Originally posted by Jason Kretzer:

What static method? Do you mean to declare the class as a static class? If you do this can you still have synchronized methods?


You can not make a class static static unless it is a nested class.


Is it possible to have methods that are both static and sychronized?


Yes, if you needed to synchronize the access to some static variables that are manipulated in a static method you can make the method synchronized.
hope that helps
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also could be considered making the fields private for being accessed only through the synchronized method
reply
    Bookmark Topic Watch Topic
  • New Topic