• 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

singlethreadmodel

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which will be thread-safe when i implement my servlet to single thread model.
1) local variable and request
2) local , instance variable and request
3) local , instance , static varaible and request.
4) only request.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is from an exam or homework question? If so, I suggest that you have a go at working out the answer yourself and let us know here. Then we can help you clear up any confusion or misunderstandings. I'm not sure that just telling you the answer will help you learn and understand.
 
yogendra singh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Frank Carver:
Is from an exam or homework question? If so, I suggest that you have a go at working out the answer yourself and let us know here. Then we can help you clear up any confusion or misunderstandings. I'm not sure that just telling you the answer will help you learn and understand.


This is the question i came across while readinf some faq. I have some knowledage of servlet and thread synchronization , but wants to get the answer of it.
What i know is that when we implement singlethread model on a servlet.
It's service() method is get thread synchronized and when a request comes for this servlet, servlet container creates an object(instance) of this servlet and intialize it by init() method and then execute the service() method. now if more then one request comes for this servlet , what servlet container will do genearlly if it wasnt implemented single thread model , it will create two thread for this single object and these thread serves both the request independently but simultaneoulsy.
now it is thread safe , then servlet container will not create threads for the servlet instead one request obtaine the control over object and this control will goes to next request when execution of service() method done.
Now plz explain how the servlet container keep another request in queue and is only variable having scope of service() method and service () method are thread-safe ...what abt another variable..

Thanks
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Local variables are not affected by multiple threads.Only instance variables and static variables can be made thread safe by implementing SingleThread model.But again if we consider pool of servlet instances,singlethreadmodel fails.Correct me if I am wrong.
Veena
[ January 08, 2004: Message edited by: Veena Point ]
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Almost.
  • Local variables and "Request" and "Page" attributes are probably safe (unless they are a reference to an unsafe object), with or without SingleThreadModel
  • instance (member) variables are normally unsafe, but can be made safer using SingleThreadModel
  • static (class) variables and "Application" and "Session" attributes are never safe.

  •  
    yogendra singh
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Veena Point:
    Local variables are not affected by multiple threads.Only instance variables and static variables can be made thread safe by implementing SingleThread model.But again if we consider pool of servlet instances,singlethreadmodel fails.Correct me if I am wrong.
    Veena
    [ January 08, 2004: Message edited by: Veena Point ]


    What is pool of servlet ? when and how singlethreadmodel fails ?
    Please explain. I need to clear this concept. waiting here for reply.
     
    Frank Carver
    Sheriff
    Posts: 7001
    6
    Eclipse IDE Python C++ Debian Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    When a request is received for a servlet marked as SingleThreadModel the server can deal with it in one of three ways:
  • The server might create (and call init()) a completely new servlet instance just for this request, and destroy it as soon as the request is finshed. This is effective, but can be very slow.
  • The server might create (and call init()) only one instance of the servlet and use it for all requests. In this case if a request for the servlet arrives while another one is still processing, the server must either queue the request, or return an error to the browser.
  • The server might create more than one instance of the servlet, and if a request comes in while one instance is busy, the server will pass the request to an instance which is currently idle. This group of pre-created servlet instances is known as a pool, and is the most popular way for servers to handle this problem.

  •  
    yogendra singh
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Frank Carver:
    When a request is received for a servlet marked as SingleThreadModel the server can deal with it in one of three ways:

  • The server might create (and call init()) a completely new servlet instance just for this request, and destroy it as soon as the request is finshed. This is effective, but can be very slow.
  • The server might create (and call init()) only one instance of the servlet and use it for all requests. In this case if a request for the servlet arrives while another one is still processing, the server must either queue the request, or return an error to the browser.
  • The server might create more than one instance of the servlet, and if a request comes in while one instance is busy, the server will pass the request to an instance which is currently idle. This group of pre-created servlet instances is known as a pool, and is the most popular way for servers to handle this problem.



  • so when the server creates pool of sevlet....all the request can execute the service() method simultaneously...so the considerable piece of code in the service () method which we wanted to thread-safe i.e. executed once at a time ( e.g a database updation or a file writing) now no more thraed-safe. isn't it seems that singlethreadmodel lossing its purpose..???
    the only thing that i can see is the instance variable...all the request will have their own instance variable and that wont get affected by others.
    one more techincal question is that when we implement singleThraedModel , they say it synchronized the service() method ...then how can be instance variable are thread-safe....
    Thanks a lot for providing such a nice clear-cut picture..
     
    Frank Carver
    Sheriff
    Posts: 7001
    6
    Eclipse IDE Python C++ Debian Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    so when the server creates pool of sevlet....all the request can execute the service() method simultaneously
    Yes, but in different objects.
    ...so the considerable piece of code in the service () method which we wanted to thread-safe i.e. executed once at a time ( e.g a database updation or a file writing) now no more thraed-safe. isn't it seems that singlethreadmodel lossing its purpose..???
    The "purpose" of SingleThreadModel is to allow you to make use of instance variables in a thread safe way. It still achieves that purpose.
    the only thing that i can see is the instance variable...all the request will have their own instance variable and that wont get affected by others.
    Exactly. That's why I say that instance variables are the only ones mad "safe" by using SIngleThreadModel in my list, above.
    one more techincal question is that when we implement singleThraedModel , they say it synchronized the service() method ...then how can be instance variable are thread-safe....
    Becuase each thread operates on a separate servlet object.
     
    yogendra singh
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by yogendra singh:
    [QB]

    one more techincal question is that when we implement singleThraedModel , they say it synchronized the service() method ...then how can be instance variable are thread-safe....

    Becuase each thread operates on a separate servlet object.

    Each thread operates seaparate servlet "object "??? in singleThreadModel we have only one "Object" or "instance" of servlet .... and when u say instance variable ... these are the variables which declare in the init () or declared in the service() method. ....

     
    Author and all-around good cowpoke
    Posts: 13078
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Each thread operates seaparate servlet "object "??? in singleThreadModel we have only one "Object" or "instance" of servlet .... and when u say instance variable ... these are the variables which declare in the init () or declared in the service() method. ....


    This is completely backwards. In single thread model, each request thread has an instance of the servlet to itself - it is the normal servlet that has only one instance shared by all request threads.
    A variable declared inside a method is a "local" variable, not an instance variable.
    Bill
     
    Frank Carver
    Sheriff
    Posts: 7001
    6
    Eclipse IDE Python C++ Debian Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I think you may be misunderstanding things a little.
    In regular (non-SingleThreadModel) servlets, there is only one instance of a servlet. Many threads can be running through this code at any time. So the code needs to be fully thread-safe.
    A servlet marked as SingleThraedModel will only have a single request thread running through it at any time. As I said in a previous reply, his may be achieved by several means:
  • creating a new servlet instance for each request thread
  • blocking each new request thread from entering the "service" method until the previous one has finshed
  • pre-creating a "pool" of servlet instances, and passing each request thread to the next free instance in the pool


  • In cases 1 and 3, there can be more than one instance of a servlet. In case 2 there will be only one, and incoming requests may have to wait for it to become ready. Container developers are free to choose any of the three options, but most seem to prefer using option 3, the servlet instance pool.
    Is that clearer?
     
    yogendra singh
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes !!! Thanks now its more clear.
    now tell me which one is bettr..
    Connection con = null;
    try{
    // open the connection here
    // some code with database communication
    // now in the end close the connection
    con.close();
    }catch(SQLException sqe) {
    con.close();
    }catch(Exception e){
    con.close();
    }
    ***** OR ****
    try{
    // open the connection here
    // some code with database communication
    // now in the end close the connection
    con.close();
    }catch(SQLException sqe) {
    }catch(Exception e){
    }
    finally {
    con.close();
    }

    IS there any performance overhead with finally block ???
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic