This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Calling explicitly destroy method in service method of servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling explicitly destroy method in service method of servlet?" Watch "Calling explicitly destroy method in service method of servlet?" New topic
Author

Calling explicitly destroy method in service method of servlet?

Nagaraj Shivaklara
Ranch Hand

Joined: Dec 16, 2008
Posts: 77
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

Thanks n Regards,
Nagaraj S K
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nagaraj Shivaklara
Ranch Hand

Joined: Dec 16, 2008
Posts: 77
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
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3011
    
  34

Servlets and wait/notify methods are now not in SCJP.
Moving to JiG.


Founder of ExamLab and Systemup
See how I can help you to become an awesome programmer
Nagaraj Shivaklara
Ranch Hand

Joined: Dec 16, 2008
Posts: 77
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
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3011
    
  34

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

Joined: Dec 16, 2008
Posts: 77
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
 
 
subject: Calling explicitly destroy method in service method of servlet?
 
Similar Threads
Thread with synchronized run() method and sleep()
Thread question doubt
Blocked vs Waiting Thread
Some Thread Synchornization questions ?
Syntax for wait/notify