File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Soft Skills this week in the Jobs Discussion 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: 42916
    
  68
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.
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?