Threading - ASP. Net vs JSP/Servlets

Discussion in 'ASP .Net' started by Peter Beck, Nov 19, 2003.

  1. Peter Beck

    Peter Beck Guest

    Hi -

    I have a question for someone who has experience with both ASP .Net and
    JSP/Servlets, relating specifically to thread safetly.

    In JSP, we say that instance vaiables of a servlet are not thread safe.
    Several threads could be running the same servlet at the same time, because
    a new thread of a servlet - as opposed to a new servlet itself - is created
    by requests for the servlet. Since there is only one instance of the
    servlet object, there is only one set of instance variables, and each thread
    could set them to values that are in conflict with those in another thread.
    However, you can write thread-safe servlets by avoiding instance variables,
    or by judicious use of the synchronize(){ } construction.

    It follows from this that instance variables in JSP pages are also not
    thread safe, since the JSP is simply turned into a servlet. In theory, if
    two people are running the same JSP page at the very close to the same time
    the second person to access the page could re-set the value of an instance
    variable to something that causes improper results for the first person, who
    may be only part way through the execution of the page. I believe there is
    a directive you can use to "sychronize" the whole page, although many
    authorities seem to discourage this.

    Can anyone tell me if there is a similar issue with ASP .Net pages,
    specifically variables in the "code-behind" page? Is there something
    analagous to a "servlet" that is created by ASP .Net, of which only one copy
    exists that many users are executing in different threads? Is there the
    possibility that users will "step on" each other's instance variables (as
    there is only one instance of the object)?

    Regards to all -

    Peter Beck
     
    Peter Beck, Nov 19, 2003
    #1
    1. Advertising

  2. Peter,

    I'm just beginning to learn JSP (the company I work for bought me a
    Powerbook G4 to play with about a month ago and I'm just getting started),
    so I may not be the best person to answer this, but I think I can.

    Each ASP.Net page is threadsafe. All variables in the code-behind page are
    threadsafe. The best equivalent to a servlet, of which only one copy exists
    that many users are executing in different threads, that I can think of
    would be an application level object. For example: If I create an object in
    the global.asax page and then assign that object to an application level
    variable, then that object is not threadsafe.

    I hope this helps.

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    Free code library at:
    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzche


    "Peter Beck" <> wrote in message
    news:...
    > Hi -
    >
    > I have a question for someone who has experience with both ASP .Net and
    > JSP/Servlets, relating specifically to thread safetly.
    >
    > In JSP, we say that instance vaiables of a servlet are not thread safe.
    > Several threads could be running the same servlet at the same time,

    because
    > a new thread of a servlet - as opposed to a new servlet itself - is

    created
    > by requests for the servlet. Since there is only one instance of the
    > servlet object, there is only one set of instance variables, and each

    thread
    > could set them to values that are in conflict with those in another

    thread.
    > However, you can write thread-safe servlets by avoiding instance

    variables,
    > or by judicious use of the synchronize(){ } construction.
    >
    > It follows from this that instance variables in JSP pages are also not
    > thread safe, since the JSP is simply turned into a servlet. In theory, if
    > two people are running the same JSP page at the very close to the same

    time
    > the second person to access the page could re-set the value of an instance
    > variable to something that causes improper results for the first person,

    who
    > may be only part way through the execution of the page. I believe there

    is
    > a directive you can use to "sychronize" the whole page, although many
    > authorities seem to discourage this.
    >
    > Can anyone tell me if there is a similar issue with ASP .Net pages,
    > specifically variables in the "code-behind" page? Is there something
    > analagous to a "servlet" that is created by ASP .Net, of which only one

    copy
    > exists that many users are executing in different threads? Is there the
    > possibility that users will "step on" each other's instance variables (as
    > there is only one instance of the object)?
    >
    > Regards to all -
    >
    > Peter Beck
    >
    >
     
    S. Justin Gengo, Nov 19, 2003
    #2
    1. Advertising

  3. Overall, the Page variables are a bad example, as the actual variable values
    are stored on each client, in ViewState. If ViewState is off, then nothing
    is saved on the client, so there is no issue. In reality, there is no issue
    either way. Some do not like to work with ViewState.

    From the way you have written this, I am not sure whether we are talking the
    same thing when we say "thread safety," but I will run with the

    If you write the following Class

    public class SessionVars
    {
    public static string SessionID;
    }

    If you set this with user #1, and then user #2 comes in and you reset it,
    both users are now effectively user #2. This is not a good thing. As such,
    you need to set up instance variables (Shared = VB.NET; static = C#) on
    application level properties, not on session level. I have personally seen
    this happen in an application and it is not pretty. If you have instance
    properties, they should be "global" in scope. I am sure someone will come up
    with an exception, but learn the rule before you break it.

    Another good reasoning for instances methods is helper functions. This is
    more a performance issue, as loading the helper function once, in memory, is
    more efficient that instancing a bunch of classes.

    On the Page class (meaning your own page, which is derived from the FCL Page
    class), I am fairly certain you could bump two sessions into each other with
    instance methods. I cannot think of a good reason, overall, to use an
    instance method, unless you have a setting for the page that should remain
    static. As the weight of setting this value when creating the instance is
    very low, I am not sure I can rubber stamp and instance method in a class.
    If someone has a good example, I would love to see it.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    **********************************************************************
    Think Outside the Box!
    **********************************************************************
    "Peter Beck" <> wrote in message
    news:...
    > Hi -
    >
    > I have a question for someone who has experience with both ASP .Net and
    > JSP/Servlets, relating specifically to thread safetly.
    >
    > In JSP, we say that instance vaiables of a servlet are not thread safe.
    > Several threads could be running the same servlet at the same time,

    because
    > a new thread of a servlet - as opposed to a new servlet itself - is

    created
    > by requests for the servlet. Since there is only one instance of the
    > servlet object, there is only one set of instance variables, and each

    thread
    > could set them to values that are in conflict with those in another

    thread.
    > However, you can write thread-safe servlets by avoiding instance

    variables,
    > or by judicious use of the synchronize(){ } construction.
    >
    > It follows from this that instance variables in JSP pages are also not
    > thread safe, since the JSP is simply turned into a servlet. In theory, if
    > two people are running the same JSP page at the very close to the same

    time
    > the second person to access the page could re-set the value of an instance
    > variable to something that causes improper results for the first person,

    who
    > may be only part way through the execution of the page. I believe there

    is
    > a directive you can use to "sychronize" the whole page, although many
    > authorities seem to discourage this.
    >
    > Can anyone tell me if there is a similar issue with ASP .Net pages,
    > specifically variables in the "code-behind" page? Is there something
    > analagous to a "servlet" that is created by ASP .Net, of which only one

    copy
    > exists that many users are executing in different threads? Is there the
    > possibility that users will "step on" each other's instance variables (as
    > there is only one instance of the object)?
    >
    > Regards to all -
    >
    > Peter Beck
    >
    >
     
    Cowboy \(Gregory A. Beamer\), Nov 19, 2003
    #3
  4. He is not using the wording thread safe the way you are thinking. The
    question deals with whether or not you can muck up instance variables from
    different sessions, which is, in fact, the case.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    **********************************************************************
    Think Outside the Box!
    **********************************************************************
    "S. Justin Gengo" <> wrote in message
    news:%...
    > Peter,
    >
    > I'm just beginning to learn JSP (the company I work for bought me a
    > Powerbook G4 to play with about a month ago and I'm just getting started),
    > so I may not be the best person to answer this, but I think I can.
    >
    > Each ASP.Net page is threadsafe. All variables in the code-behind page are
    > threadsafe. The best equivalent to a servlet, of which only one copy

    exists
    > that many users are executing in different threads, that I can think of
    > would be an application level object. For example: If I create an object

    in
    > the global.asax page and then assign that object to an application level
    > variable, then that object is not threadsafe.
    >
    > I hope this helps.
    >
    > --
    > Sincerely,
    >
    > S. Justin Gengo, MCP
    > Web Developer / Programmer
    >
    > Free code library at:
    > www.aboutfortunate.com
    >
    > "Out of chaos comes order."
    > Nietzche
    >
    >
    > "Peter Beck" <> wrote in message
    > news:...
    > > Hi -
    > >
    > > I have a question for someone who has experience with both ASP .Net and
    > > JSP/Servlets, relating specifically to thread safetly.
    > >
    > > In JSP, we say that instance vaiables of a servlet are not thread safe.
    > > Several threads could be running the same servlet at the same time,

    > because
    > > a new thread of a servlet - as opposed to a new servlet itself - is

    > created
    > > by requests for the servlet. Since there is only one instance of the
    > > servlet object, there is only one set of instance variables, and each

    > thread
    > > could set them to values that are in conflict with those in another

    > thread.
    > > However, you can write thread-safe servlets by avoiding instance

    > variables,
    > > or by judicious use of the synchronize(){ } construction.
    > >
    > > It follows from this that instance variables in JSP pages are also not
    > > thread safe, since the JSP is simply turned into a servlet. In theory,

    if
    > > two people are running the same JSP page at the very close to the same

    > time
    > > the second person to access the page could re-set the value of an

    instance
    > > variable to something that causes improper results for the first person,

    > who
    > > may be only part way through the execution of the page. I believe there

    > is
    > > a directive you can use to "sychronize" the whole page, although many
    > > authorities seem to discourage this.
    > >
    > > Can anyone tell me if there is a similar issue with ASP .Net pages,
    > > specifically variables in the "code-behind" page? Is there something
    > > analagous to a "servlet" that is created by ASP .Net, of which only one

    > copy
    > > exists that many users are executing in different threads? Is there the
    > > possibility that users will "step on" each other's instance variables

    (as
    > > there is only one instance of the object)?
    > >
    > > Regards to all -
    > >
    > > Peter Beck
    > >
    > >

    >
    >
     
    Cowboy \(Gregory A. Beamer\), Nov 19, 2003
    #4
  5. Ahhh, then I was right about not knowing enough JSP yet.

    Thanks. (I'll keep studying.)

    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    Free code library at:
    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzche


    "Cowboy (Gregory A. Beamer)" <> wrote in
    message news:O$...
    > He is not using the wording thread safe the way you are thinking. The
    > question deals with whether or not you can muck up instance variables from
    > different sessions, which is, in fact, the case.
    >
    > --
    > Gregory A. Beamer
    > MVP; MCP: +I, SE, SD, DBA
    >
    > **********************************************************************
    > Think Outside the Box!
    > **********************************************************************
    > "S. Justin Gengo" <> wrote in message
    > news:%...
    > > Peter,
    > >
    > > I'm just beginning to learn JSP (the company I work for bought me a
    > > Powerbook G4 to play with about a month ago and I'm just getting

    started),
    > > so I may not be the best person to answer this, but I think I can.
    > >
    > > Each ASP.Net page is threadsafe. All variables in the code-behind page

    are
    > > threadsafe. The best equivalent to a servlet, of which only one copy

    > exists
    > > that many users are executing in different threads, that I can think of
    > > would be an application level object. For example: If I create an object

    > in
    > > the global.asax page and then assign that object to an application level
    > > variable, then that object is not threadsafe.
    > >
    > > I hope this helps.
    > >
    > > --
    > > Sincerely,
    > >
    > > S. Justin Gengo, MCP
    > > Web Developer / Programmer
    > >
    > > Free code library at:
    > > www.aboutfortunate.com
    > >
    > > "Out of chaos comes order."
    > > Nietzche
    > >
    > >
    > > "Peter Beck" <> wrote in message
    > > news:...
    > > > Hi -
    > > >
    > > > I have a question for someone who has experience with both ASP .Net

    and
    > > > JSP/Servlets, relating specifically to thread safetly.
    > > >
    > > > In JSP, we say that instance vaiables of a servlet are not thread

    safe.
    > > > Several threads could be running the same servlet at the same time,

    > > because
    > > > a new thread of a servlet - as opposed to a new servlet itself - is

    > > created
    > > > by requests for the servlet. Since there is only one instance of the
    > > > servlet object, there is only one set of instance variables, and each

    > > thread
    > > > could set them to values that are in conflict with those in another

    > > thread.
    > > > However, you can write thread-safe servlets by avoiding instance

    > > variables,
    > > > or by judicious use of the synchronize(){ } construction.
    > > >
    > > > It follows from this that instance variables in JSP pages are also not
    > > > thread safe, since the JSP is simply turned into a servlet. In

    theory,
    > if
    > > > two people are running the same JSP page at the very close to the same

    > > time
    > > > the second person to access the page could re-set the value of an

    > instance
    > > > variable to something that causes improper results for the first

    person,
    > > who
    > > > may be only part way through the execution of the page. I believe

    there
    > > is
    > > > a directive you can use to "sychronize" the whole page, although many
    > > > authorities seem to discourage this.
    > > >
    > > > Can anyone tell me if there is a similar issue with ASP .Net pages,
    > > > specifically variables in the "code-behind" page? Is there something
    > > > analagous to a "servlet" that is created by ASP .Net, of which only

    one
    > > copy
    > > > exists that many users are executing in different threads? Is there

    the
    > > > possibility that users will "step on" each other's instance variables

    > (as
    > > > there is only one instance of the object)?
    > > >
    > > > Regards to all -
    > > >
    > > > Peter Beck
    > > >
    > > >

    > >
    > >

    >
    >
     
    S. Justin Gengo, Nov 19, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mick
    Replies:
    0
    Views:
    446
  2. Prateeksha Web Services
    Replies:
    0
    Views:
    1,804
    Prateeksha Web Services
    Aug 29, 2003
  3. bigbinc
    Replies:
    1
    Views:
    710
    John C. Bollinger
    Nov 5, 2003
  4. Ramanan

    jsp & servlets book

    Ramanan, Feb 19, 2004, in forum: Java
    Replies:
    3
    Views:
    496
    Rick Umali
    Mar 2, 2004
  5. Replies:
    0
    Views:
    4,440
Loading...

Share This Page