• 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

Calling explicitly destroy method in service method of servlet?

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

I have few questions:

1) Can we implement servlet service method and in that can we call explicitly destroy method? Will it work?

2) Can two different threads access two different synchronized methods of same object?(i mean simultaneously)

3) Say i have a synchronized method in a class and i have ten different threads with different priority and all threads are waiting to invoke the method. If running thread on this method uses notifyALL() to notify the other threads, which one will get the access? And if it uses notify() then which thread will get access?

Please answers these questions.



Thanks
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) This has nothing to do with SCJP. But yes you can explicitly call destroy method of your servlet, although you shouldn't do so. The destroy method is similar to finalize method in the sense that its intended to be called automatically not explicitly.
2) No, two different threads cannot enter two different synchronized methods on the same object.
3) notifyAll only wakes Threads who called wait method. After a thread releases the lock on an object, then any thread can be chosen to acquire the lock on that object, there are no guarantees that the thread with higher priority will get the lock on the object. If you call notify, then too its not guaranteed which thread's wait call will return...
 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ankit.



Ankit Garg wrote:1) This has nothing to do with SCJP. But yes you can explicitly call destroy method of your servlet, although you shouldn't do so. The destroy method is similar to finalize method in the sense that its intended to be called automatically not explicitly.
2) No, two different threads cannot enter two different synchronized methods on the same object.
3) notifyAll only wakes Threads who called wait method. After a thread releases the lock on an object, then any thread can be chosen to acquire the lock on that object, there are no guarantees that the thread with higher priority will get the lock on the object. If you call notify, then too its not guaranteed which thread's wait call will return...

 
Sheriff
Posts: 7145
1364
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Servlets and wait/notify methods are now not in SCJP.
Moving to JiG.
 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,

Is there any reason why we can't access two different syn methods with two different thread? i mean i am not understanding why we can't access like that?
 
Devaka Cooray
Sheriff
Posts: 7145
1364
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the first thread has owned the monitor lock of that object, the other one cannot own the monitor lock of the same object until the first thread releases it. The second thread will have to wait until the first thread leaves the synchronized method.

By the way, if the synchronized contexts are different (using synchronized(...){} blocks), it's possible that two threads can simultaneously access two code blocks of the same object.

Devaka
 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Devaka, but in synchronized code block, do we need to create another object instance? i mean one instance of the object for one syn block!! is this correct? or no need to create separate instances for each sync code blocks?

Devaka Cooray wrote:When the first thread has owned the monitor lock of that object, the other one cannot own the monitor lock of the same object until the first thread releases it. The second thread will have to wait until the first thread leaves the synchronized method.

By the way, if the synchronized contexts are different (using synchronized(...){} blocks), it's possible that two threads can simultaneously access two code blocks of the same object.

Devaka

reply
    Bookmark Topic Watch Topic
  • New Topic