It's not a secret anymore!
The moose likes Threads and Synchronization and the fly likes Looking for Base Idea behind Multithreading Concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Looking for Base Idea behind Multithreading Concept" Watch "Looking for Base Idea behind Multithreading Concept" New topic

Looking for Base Idea behind Multithreading Concept

Ankit Khandelwal

Joined: Feb 21, 2012
Posts: 13
Hi All

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.

Thanks in advance , looking for good discussion.

RatiKanta pal
Ranch Hand

Joined: Nov 13, 2011
Posts: 83

Hi ankit,(Regarding how web-server works)

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 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.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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 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.

[Jess in Action][AskingGoodQuestions]
RatiKanta pal
Ranch Hand

Joined: Nov 13, 2011
Posts: 83

hi Ernest Friedman-Hill,

Thank you for your information.

Previously i was assuming , session is one thread.and though that thread all operation carried out..Now i think its clear to me.

I agree. Here's the link:
subject: Looking for Base Idea behind Multithreading Concept
It's not a secret anymore!