Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what could be the best alternative to Multi-threading? [closed]

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.

like image 611
Anil Kumar Avatar asked Jul 07 '15 08:07

Anil Kumar


People also ask

Which is better single thread or multi-thread?

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.

What are the types of multi threading?

Multi threading model are of three types. Many to many model. Many to one model. one to one model.

Should I use threading or multiprocessing?

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.


2 Answers

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.

like image 109
Cephalopod Avatar answered Sep 19 '22 13:09

Cephalopod


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.

like image 39
eliasah Avatar answered Sep 19 '22 13:09

eliasah