Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Threads from JQuest

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey java ppl

1. class Board extends Object {
2. Image[] pics = new Image[64];
3. int active = 0;
4. public boolean addPic( Image mg, int pos) {
5. synchronized (this) {
6. if (pics[pos] == null)
7. { active++ ; pics[pos] = mg; return true; }
8. else
9. return false;
10. }
11. }
12. // remainder of class

Select all the alternatives for line 5 that would accomplish this.
choices given
1) synchronized(this)
2) synchronized(pic)
3) synchronized(mg)
4) synchronized(active)
answer given is 1,2 my question is why not 3???
Regards Denish
 
denish mehta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Come on guys it should'nt be that much of problem !!!
Regards Denish
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by denish mehta:
Hey java ppl
4. public boolean addPic( Image mg, int pos) {
5. synchronized (this) {
3) synchronized(mg)


option 3 would not give error,but it of no use to synchronize on a object which u got as a formal paramater.Thread will have un synchronized access to the code after synchronzed(mg),which is not of any use.
hope u understand.
pawan

 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just curious. How does one find the answers on JQuest?
Percy
 
Your mother is a hamster and your father smells of tiny ads!
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic