• 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

Need help for k&b book: thread question...

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
on page 754 (answer of Q15):
"....B,D,E and G could all happen because instance methods and static methods lock on different objects, and do not block each other"

Which objects that means..?

the object of an instance method ( void a()) : this ?
the object of a static method ( static void b()) : ThreadDemo.class ?

If "void b()" is not a static method,say an instance method like a(), "B" and "E" will be also the correct answers,am I right?

thanks
[ May 28, 2006: Message edited by: Xinhua Gu ]
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



"....B,D,E and G could all happen because instance methods and static methods lock on different objects, and do not block each other"



answer A, F, H are correct.
Two point shud be remembered for dat.

1. If one thread is accessing a non-static synchronized method, then no other thread can access same or ny other non-static synchronized method but can access static synchronized method.

2. If one thread is accessing a static synchronized method, then no other thread can access same or ny other static synchronized method but can access non-static synchronized methods.

jst remember these two cases n u will get the result.

Here I m giving a sample code where...

1. Thread1 is calling non-static synchronized method a() in infinite loop.
2. & Thread2 is calling static synchronized method b() in infinite loop.

Remember sleep(int millisec) method does not release the lock.



output of this code is...
In a
In b
In a
In b
.
.
.

From the output its clear that thread2 can access method b() even if thread1 is in method a() becoz method a() is non-static and method b() is static. Thread1 is having an object lock and thread2 is having class lock and these two locks are not same. They are different.

If I remove static from method b() then thread2 can not enter in method b() becoz now both require same lock (i.e. object's lock).




In this case output will be...

In a
In a
In a
.
.
.


regards

Naseem Khan
[ May 28, 2006: Message edited by: Naseem Khan ]
 
Xinhua Gu
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now i understood.. Thank you very much!
 
It's a tiny ad. At least, that's what she said.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic