Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling explicitly destroy method in service method of servlet?

 
Nagaraj Shivaklara
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • 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
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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...
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4682
332
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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
ExamLab Creator
Marshal
Pie
Posts: 4682
332
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic