#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    61
    Rep Power
    12

    Threading in servlets?


    I relly would like to know how a servlet is invoked.

    Is a new instance of a servlet class created to handle each request? Or is it the same instance handling all requests? What happens to data members of the servlet class when handling more then one reqest, do they need some kind of synchronization?
  2. #2
  3. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    A servlet is kind of like a class that implements Runnable (it might behind the scenes for all I know). It makes one Servlet object. For each request the app server runs the service method of that object in a new thread. All instance variables of the servlet are shared between the service method calls. The local variables in the service method (and all methods) are thread-safe. That means if you create instance variables of the object and manipulate them in the service method (or doPost/doGet) you will get threading problems.

    consider this example:
    Code:
    public class TestRunnable {
    
    	public static void main(String[] args) {
    		//one servlet created
    		myRunnable my = new myRunnable();
    		//a request comes in for this servlet
    		Thread t1 = new Thread(my);
    		t1.start();
    		//some time passes
    		try {
    			Thread.sleep(2000);
    		}
    		catch (InterruptedException e) {
    		}
    
    		//another request comes in
    		Thread t2 = new Thread(my);
    		//changes occur in an instance variable in the servlet.
    		//I have to call the method here because I cannot pass
    		//any values to the run method. Normally this change
    		//would be happening in the service method itself.
    		//This is where run and service are different. 
    		//service has stuff passed to it.
    		my.changeS("something else");
    		//now t2 outputs the new value AND t1 outputs the new value
    		t2.start();
    	}
    }
    class myRunnable implements Runnable {
    	private String s = "something";
    	/* (non-Javadoc)
    	 * @see java.lang.Runnable#run()
    	 */
    	public void run() {
    		while (true) {
    			try {
    				Thread.sleep(500);
    				System.out.println(s);
    			}
    			catch (InterruptedException e) {
    			}
    		}
    	}
    	public void changeS(String newS) {
    		s = newS;
    	}
    }
    
    NOTE: this will not stop running. You must use crtl-c to stop.
    You can protect your instance variables by synchronizing them. Note that this can have a huge performance penalty.

    Generally speaking, you should avoid any use of servlet instance variables unless they are read only. Use local variables or beans saved to a session.
    Last edited by Nemi; March 19th, 2003 at 10:19 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    61
    Rep Power
    12
    I plan to create a HTML template object in init() method, so that the template is read from file, scanned for markup etc. on startup.

    In doGet() I just use template object's copy constructor to make a local copy of the "master" object, and since this does not require file IO and scanning, it should be a bit faster (the former takes approx. 1 ms).

    Now, the thing is that the HTML template file may get updated, so the servlet needs some way to check for this, or it will continue serving the old design.

    So basically, all syncronizing has to do with refreshing template file... I don't know what is the best way to do it, but it will need some kind of sunchronizing og the copy/update. Any ideas?
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Well, you've kind of touched on the crux of the problem between performance and maintainability. You can try so hard to make a program fast that it can "break" easy if anything changes. Also, the faster you make something, generally it becomes more complex and harder to maintain. Finding a balance is always the chore.

    If you want to be able to dynamically change the html template file and have the changes reflected by the served pages without restarting the server, then you need something similar to what the app server does with class files that expire (when that option is on). Your template object could create a "helper" thread that is on a timer that periodically checks the timestamp of the html template file. At any given time if the timestamp differs from the one it was when it loaded the template originally, it reloads the template and does whatever it does for configuring.

    You would need to be careful about properly shutting this thread down when the app server is restarting or it may hang the app server. Hard to say what it will do. You could use the servlets destroy method to flag the template object that it needs to stop its helper thread.

    Hope that gives you some ideas. GL
    Last edited by Nemi; March 19th, 2003 at 11:56 AM.
  8. #5
  9. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    Another idea that might be easier at the potenital cost of some performance would be to have an instance variable in the template object that contains the timestamp. Every request the servlet could check the timestamp of the template.html and compare it to the one contained in the template object. if they differ it could reinitialize the template object. This would be a performance penalty for that request, but all further requests would not be affected. Similar to the penalty you get when the app server has to recompile a changed jsp.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    61
    Rep Power
    12
    Well, probably recompiling a JSP takes more than 1 ms

    Off course, I could just sacrifice this 1 ms and just re-load my template file on every request.

IMN logo majestic logo threadwatch logo seochat tools logo