Please help me to think about multithreading basic concept.
Let say we have a web application which is deployed on an application server , now how the multi-threading will come into picture , how does that situation comes when we say same variable is getting used by two or multiple instances or requests . when we should start thinking about multithreading during our development time. I understand synchronization , wait , sleep , notify and all those things but not able to understand exactly when do i need it? whose job is this to handle multithreading .. Web Server or Code ..
In a very short sentences , if somebody can guide me before we say i have two threads .... or Thread A is writting while Thread A is reading or something like this.
Thread:Thread is nothing but small small prog executed on perticular system.
Lets take one small example..Say take Facebok .There are 'n' numbers of users of fb.Now when you open the browser and hit fb.com a defult thread is created which is my main thread.When you login to that system another thread is created which is registered under your user id and all the opreration regarding to your profile will keep track by your thread only.Smilarly there are billion number of thread created by web-server to to give concurrent users to use one site.
Although I know RatiKanta pal is trying to be helpful here, I think I have to speak up and say that essentially all of this is factually wrong. Web servers don't create one thread per user, and Facebook.com is most certainly not running billions of threads in parallel.
Nor are all the requests for a given user handled by one thread; a small pool of threads handles requests as they come in, without concern for previous history. The number of threads in the pool is based on what the server hardware can handle, not on the number of users. State for a user is held in cookies in the web browser and and in session objects on the server; the session objects are examined by code running in a thread that handles a request.
Threads in a web server allow multiple requests to be handled at once. Multiple threads can be calling the service method of a single servlet object at the same time, and they won't interfere, as long as there is no data that must be shared between threads.
If your servlet objects have member variables whose values can change during execution, then you need to be concerned about writing multithreaded code. Most of the time, however, this isn't a concern, as you don't normally store data like that in your servlets. As I said, you store it in the session.