wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Single thread model:mock exam question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Single thread model:mock exam question?" Watch "Single thread model:mock exam question?" New topic
Author

Single thread model:mock exam question?

Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Source : http://www.podar.net/cgi-bin/scwcd/answers.pl?question=33&correct=4&user=1

1. public class TestServlet extends HttpServlet implements SingleThreadModel {

2. private static int num = 5;

3. public void doGet(HttpServletRequest req, HttpServletResponse resp) {

4. // do nothing;

5. }

6. }


Is num variable thread safe?

Explanation: Explanation though the class implements SingleThreadModel but since the variable declared is static ,there is still the risk that num is modified by multiple pool instances or multiple named instances simultaneously

I dont understand ? num should be thread safe, as there will be only one thread of this servlet at a time.
[ August 08, 2008: Message edited by: Sunny Jain ]

Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41137
    
  45
as there will be only one thread of this servlet at a time.

No. There will only be one thread using this servlet instance. But as the answer says, there can be multiple instances, and they all share the same static variables.


Ping & DNS - my free Android networking tools app
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Thanks..! I got it now..!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Single thread model:mock exam question?
 
Similar Threads
Which of the following statements is correct regarding the servlets that implement
Single Thread Model
Threads with static variables
SingleThreadModel question
Urgent : Thread Safe