Currently we are using threads in our application(Java). But there will be some 1000 (or more) threads created at a time.This threads should process the data and store it in db.
This is consuming more memory and I/O.
What could be the best alternative for this?. Scalability,Consistency and performance are the main requirements.
The main difference between single thread and multi thread in Java is that single thread executes tasks of a process while in multi-thread, multiple threads execute the tasks of a process. A process is a program in execution. Process creation is a resource consuming task.
Multi threading model are of three types. Many to many model. Many to one model. one to one model.
Multiprocessing is a easier to just drop in than threading but has a higher memory overhead. If your code is CPU bound, multiprocessing is most likely going to be the better choice—especially if the target machine has multiple cores or CPUs.
Have you tried thread pools? A thread pool consists of a reasonable number of threads (enough to use all processors, but not much more) and re-uses threads (again reducing overhead) to execute a large number of tasks concurrently.
Here is a small example to give you an idea
ExecutorService executor = Executors.newFixedThreadPool(5);
Runnable job = new Runnable() {
public void run() {
// do some work
}
}
executor.execute(job);
If you look at the ScheduledThreadPoolExecutor
, you will find a lot of features for executing and scheduling jobs.
Try to take a look at the Actor model.
The actor model is a concurrent programming model, in which the workload is distributed between entities running in parallel, called actors.
It is a model in which there is no shared state, actors are isolated and information can flow in the form of messages.
The players receive these messages and can only react manipulating the data in the message (computing or processing on data), sending a message to other players or by creating new actors.
This model is a high level abstraction over mutex-locks and threads, which removes the complexity for the developer and it was designed mainly to build highly available and competing telecom systems, by Ericsson in 1973 on Erlang.
Actors are very lightweight concurrent entities. They process messages asynchronously using an event-driven receive loop. Pattern matching against messages is a convenient way to express an actor's behavior. They raise the abstraction level and make it much easier to write, test, understand and maintain concurrent and/or distributed systems. You can focus on workflow—how the messages flow in the system—instead of low level primitives like threads, locks and socket IO.
In Java/Scala, you can find the Akka framework that is build based on this actor model.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With