if a single class can be performed concurrently by multiple thread. Why EJB pooled stateless bean in container? - ejb

a single object in JVM can be run simultaneously by multiple threads. same bytecode will run in different threads.
keeping thread count same if we increase object or pooled object in the container do you think performance will increase? if yes then how?
if no then why EJB use the stateless polled objects to serve concurrent requests?

Using the pool size you control how many threads can run the code of a bean in parallel. This can be important, e.g. if the maximum number of DBMS-Connections is limited.
On the other hand, if the poolsize would be greater than the available number of threads, then the pool will never get exhausted.

Related

Mule 4 Async vs VM Scope, which is more preferred to use for processing flow asynchronously?

from what I can comprehend briefly, both of them processing flow asynchronously with VM scope using more resource as it create new context, separate properties and variables. Any particular reason other than that if the use is just to process the flow asynchronously?
Async is a scope that is executed immediately in parallel with respect to the flow, if there are resources available (ie threads). VM is a connector that implements an in-memory queue. I usually recommend to prefer to use the VM connector because with Async if there are no threads available it can fail to execute. With the VM connector the messages will be queued until the flow that reads from the VM queue is able to read the next message. Note that if the number of messages queued is greater than the number of messages processed it will run out of memory or exceed the queue allocation, causing another error.
Always remember that threads are a limited resource. In Mule it is not possible to control the number of threads used, only the concurrency. Also keep in mind that threads are not free, they consume memory and CPU.

async await advantages when we have enough threads

I understood that .net know to use multiple threads for multiple requests.
So, if probably our service wont get more request than the number of threads our server can produce (it look like huge number), the only reason I can see to use async is on single request that do multiple blocking operations which can done in parallel.
Am I right?
Another advantage may be that serve multiple requests with same thread is cheaper than use multiple threads. How significant is this difference?
(note: no UI exists in our service (I saw that there is single thread for this, but it isn't relevant))
thanks!
Am I right?
No, doing multiple independent blocking operations, is the job of Concurrent APIs anyway (though sometimes they need Synchronization (like lock, mutex) to maintain the object state and avoid Race condition), but the usage of Async-Await is to schedule the IO Operations, like File Read / Write, call a remote service or Database Read / Write, which doesn't need a thread, as they are queued on a queue in hardware called IO Completion ports.
Benefits of Async-Await:
Doesn't start a IO operation on a separate Thread, since Thread is a costly resource, in terms memory and resource allocation and would do little precious than wait for IO call to come back. Separate thread shall be used for the compute bound operations, no IO bound.
Free up the UI / caller thread to make it completely responsive to carry out other tasks / operations
This is the evolution of Asynchronous programming model (BeginXX, EndXX), which was fairly complex to understand and implement
Another advantage may be that serve multiple requests with same thread is cheaper than use multiple threads. How significant is this difference?
Its a good strategy depending on the kind of request from caller, if they are compute bound better invoke a Parallel API and finish them fast, IO bound there's Async-Await, only issue with multiple threads is Resource allocation and Context switching, which needs to be factored in, but on other end it efficiently utilize the processor cores, which are fairly under utilized in the current day systems, as you would see most of the time processor is lying idle

EJB 3.1 asynchronous method and thread pool

I need to process about 250.000 documents per day with an EJB 3.1 asynchronous method in order to face an overall long time task.
I do this to use more threads and process more documents concurrently. Here's an example in pseudo code:
// this returns about 250.000 documents per day
List<Document> documentList = Persistence.listDocumentsToProcess();
for(Document currentDocument: documentList){
//this is the asynchronous call
ejbInstance.processAsynchronously(currentDocument);
}
Suppose I have a thread pool of size 10 and 4 core processors, my questions are:
how many documents will the application server process SIMULTANEOUSLY?
what happen when all thread in pool are processing a documents and one more asynchronous call comes? Will this work like a sort of JMS Queue?
would I have any improvement adopting a JMS Queue solution
I work with Java EE 6 and WebSphere 8.5.5.2
The default configuration for asynchronous EJB method calls is as follows (from the infocenter):
The EJB container work manager has the following thread pool settings:
Minimum number of threads = 1
Maximum number of threads = 5
Work request queue size = 0 work objects
Work request queue full action = Block
Remote Future object duration = 86400 seconds
So trying to answer your questions:
how many documents will the application server process SIMULTANEOUSLY? (assuming 10 size thread pool)
This thread pool is for all EJB async calls, so first you need to assume that your application is the only one using EJB async calls. Then you will potentially have 10 runnable instances, that will be processed in parallel. Whether they will be processed concurrently depends on the number of cores/threads available in the system, so you cant have accurate number (some cores/threads may be doing web work for example, or other process using cpu).
what happen when all thread in pool are processing a documents and one more asynchronous call comes?
It depends on the Work request queue size and Work request queue full action, settings. If there are no available threads in the pool, then requests will be queued till the queue size is reached. Then it depends on the action, which might be Block or Fail.
would I have any improvement adopting a JMS Queue solution
Depends on your needs. Here are some pros/cons JMS solution.
Pros:
Persistence - if using JMS your asynchronous task can be persistent, so in case of the server failure you will not lost them, and will be processed after restart or by other cluster member. EJB async queue is held only in memory, so tasks in queue are lost in case of failure.
Scalability - if you put tasks to the queue, they might be concurrently processed by many servers in the cluster, not limited to single JVM
Expiration and priorities - you can define different expiration time or priorities for your messages.
Cons:
More complex application - you will need to implement MDB to process your tasks.
More complex infrastructure - you will need database to store the queues (file system can be used for single server, and shared filesystem can be used for clusters), or external messaging solution like WebSphere MQ
a bit lower performance for processing single item and higher load on server, as it will have to be serialized/deserialized to persistent storage

Is MSDN referencing a system.thread, a worker thread, an I/O thread or all three?

Please see the warning below taken from the StreamWriter class specification:
"Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe."
I understand that a W3WC process contains two thread pools i.e. worker threads and I/O threads. A worker thread could contain many threads of its own (if the application creates its own System.Thread instances).
Does the warning only relate to System.Threads or does it relate to worker threads and I/O threads as well I.e. as the instance variables of the StreamWriter class are not thread safe then does this mean that there would be problems if multiple worker threads access it, e.g. if two users on two different web clients attempt to write to the log file at the same time, then could one lock out the other?
If a class isn't threadsafe, then you can't take an instance of that class and use that instance from more than one thread. It doesn't matter whether they're System.Threading.Thread or ThreadPool or Task or worker threads within IIS. They're all threads -- they're all preemptive multitasking while the object is in a state where it doesn't expect to be preempted.
That doesn't matter in the scenario you describe, though. Suppose two Web clients try to connect to the server at the same time, and your request handler tries to log to a file, meaning you've got two threads potentially trying to write to that file simultaneously. But that's not a thread-safety issue, because you wouldn't be using the same StreamWriter instance in both threads. (At least, I hope not -- if you have a static StreamWriter instance, how would you ever know when to flush it and close the file? Sharing the same writer across threads wouldn't make any sense.)
Instead, each thread should create its own StreamWriter, write to it, and then close it. Yes, there are still concurrency issues -- if the first thread hasn't closed the file yet when the second thread tries to open the file, then the second thread would get a file-sharing exception, and you would need to catch that exception and retry -- but that's a file-locking issue, not a thread-safety issue.

Will using Parallel.ForEach in ASP.NET prevent other threads from processing requests?

I'm looking at using some PLINQ in an ASP.NET application to increase throughput on some I/O bound and computationally bound processing we have (e.g., iterating a collection to use for web service calls and calculating some geo-coordinate distances).
My question is: should I be concerned that running operations like Parallel.ForEach will take threads away from processing other requests, or does it use a separate pool of threads (I've heard about something called I/O completion ports, but not sure how that plays into the discussion)?
Parallel.ForEach will, at most, use one thread for as many cores as you have. The thread pool default maximum size is 250 times the number of cores you have. So you'll have to be really trying to run out of available threads.

Resources