{{theTime}}

Search This Blog

Total Pageviews

Servlets multi threaded Service method


Threading
A servlet must be able to handle multiple concurrent requests. Any number of end users at any given time could invoke the servlet, and while the init method is always run single-threaded, the servicemethod is multi-threaded to handle multiple requests.
This means any static or public fields accessed by the service method should be restricted to simple thread access. The example below uses the synchronized keyword to restrict access to a counter so it can only be updated by one thread at a time:
  int counter
  Boolean lock = new Boolean(true);

  synchronized(lock){
    counter++;
  }

No comments:

Generate Insert Sql from Select Statement

SELECT 'INSERT INTO ReferenceTable (ID, Name) VALUES (' +        CAST(ID AS NVARCHAR) + ', ''' + Name + ''...