javaAndBeyond
javaAndBeyond

Reputation: 540

Multithreading - multiple users

When a single user is accessing an application, multiple threads can be used, and they can run parallel if multiple cores are present. If only one processor exists, then threads will run one after another.

When multiple users are accessing an application, how are the threads handled?

Upvotes: 9

Views: 4394

Answers (7)

pedrohreis
pedrohreis

Reputation: 1090

You need understand about thread scheduler. In fact, in a single core, CPU divides its time among multiple threads (the process is not exactly sequential). In a multiple core, two (or more) threads can run simultaneously. Read thread article in wikipedia. I recommend Tanenbaum's OS book.

Upvotes: 4

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21748

In "classic" implementations, all web requests arriving to the same port are first serviced by the same single thread. However as soon as request is received (Socket.accept returns), almost all servers would immediately fork or reuse another thread to complete the request. Some specialized single user servers and also some advanced next generation servers like Netty may not.

The simple (and common) approach would be to pick or reuse a new thread for the whole duration of the single web request (GET, POST, etc). After the request has been served, the thread likely will be reused for another request that may belong to the same or different user.

However it is fully possible to write the custom code for the server that binds and then reuses particular thread to the web request of the logged in user, or IP address. This may be difficult to scale. I think standard simple servers like Tomcat typically do not do this.

Upvotes: 0

mhyst
mhyst

Reputation: 297

There is no difference if you have one user or several. Threads work depending the logic of your program. The processor runs every thread for a certain ammount of time and then follows to the next one. The time is very short, so if there are not too much threads (or different processes) working, the user won't notice it. If the processor uses a 20 ms unit, and there are 1000 threads, then every thread will have to wait for two seconds for its next turn. Fortunately, current processors, even with just one core, have two process units which can be used for parallel threads.

Upvotes: 0

Ravindra babu
Ravindra babu

Reputation: 38910

There are two points to answer to your question : Thread Scheduling & Thread Communication

Thread Scheduling implementation is specific to Operating System. Programmer does not have any control in this regard except setting priority for a Thread.

Thread Communication is driven by program/programmer.

Assume that you have multiple processors and multiple threads. Multiple threads can run in parallel with multiple processors. But how the data is shared and accessed is specific to program.

You can run your threads in parallel Or you can wait for threads to complete the execution before proceeding further (join, invokeAll, CountDownLatch etc.). Programmer has full control over Thread life cycle management.

Upvotes: 1

rpy
rpy

Reputation: 4013

The incorrect assuption is

If only one processor exists, then threads will run one after another.

How threads are being executed is up to the runtime environment. With java there are some definitions that certain parts of your code will not be causing synchronisation with other threads and thus will not cause (potential) rescheduling of threads.

In general, the OS will be in charge of scheduling units-of-execution. In former days mostly such entities have been processes. Now there may by processes and threads (some do scheduling only at thread level). For simplicity let ssume OS is dealing with threads only.

The OS then may allow a thread to run until it reaches a point where it can't continue, e.g. waiting for an I/O operation to cpmplete. This is good for the thread as it can use CPU for max. This is bad for all the other threads that want to get some CPU cycles on their own. (In general there always will be more threads than available CPUs.So, the problem is independent of number of CPUs.) To improve interactive behaviour an OS might use time slices that allow a thread to run for a certain time. After the time slice is expired the thread is forcible removed from the CPU and the OS selects a new thread for being run (could even be the one just interrupted).

This will allow each thread to make some progress (adding some overhead for scheduling). This way, even on a single processor system, threads my (seem) to run in parallel.

So for the OS it is not at all important whether a set of thread is resulting from a single user (or even from a single call to a web application) or has been created by a number of users and web calls.

Upvotes: 6

Mahesh Kumar Chopker
Mahesh Kumar Chopker

Reputation: 142

I can talk from Java perspective, so your question is "when multiple users are accessing an application, how are the threads handled?". The answer is it all depends on how you programmed it, if you are using some web/app container they provide thread pool mechanism where you can have more than one threads to server user reuqests, Per user there is one request initiated and which in turn is handled by one thread, so if there are 10 simultaneous users there will be 10 threads to handle the 10 requests simultaneously, now we do have Non-blocking IO now a days where the request processing can be off loaded to other threads so allowing less than 10 threads to handle 10 users.

Now if you want to know how exactly thread scheduling done around CPU core, it again depends on the OS. One thing common though 'thread is the basic unit of allocation to a CPU'. Start with green threads here, and you will understand it better.

Upvotes: 7

Bhushan Bhangale
Bhushan Bhangale

Reputation: 10987

Tomcat uses Java multi-threading support to serve http requests.

To serve an http request tomcat starts a thread from the thread pool. Pool is maintained for efficiency as creation of thread is expensive.

Refer to java documentation about concurrency to read more https://docs.oracle.com/javase/tutorial/essential/concurrency/

Please see tomcat thread pool configuration for more information https://tomcat.apache.org/tomcat-8.0-doc/config/executor.html

Upvotes: 2

Related Questions