From the commentary to the accepted answer: "Could you advise this?"
Yes, do not use streams directly. Starting with java 5, we have the java.util.concurrent infrastructure, which makes it easier to manage threads and tasks.
Creating a thread is expensive. This is before the thread completes the task you want, it must be created, a very long process. For this reason, we have the concept of a thread pool. Instead of creating new threads every time you want to perform a parallel task, you create the threads you need so that they are ready, and you will send them tasks when you need it. Pool threads as a second adventure. When the task is completed, the thread will not be destroyed, but will be active to run the next task, so the cost of creating a thread occurs only once during initialization.
So how do you use theses?
First create an artist with a thread pool. To stay simple, we create a thread pool with 100 threads (since you want to simulate a load of 100 simultaneous calls):
ExecutorService pool = Executors.newFixedThreadPool(100);
Then you submit your tasks:
long time = System.currentTimeMillis(); for (int i=0; i<100000; i++) { pool.execute(new Car()); }
And importantly, you will wait until all tasks are completed before the program stops.
pool.shutdown(); //Do no longer accept new tasks. pool.awaitTermination(1, TimeUnit.HOURS); //Wait for up to one hour for all tasks to finish. long completedIn = System.currentTimeMillis() - time; System.out.println(DurationFormatUtils.formatDuration(completedIn, "HH:mm:ss:SS"));
In your thread code, you did not wait for the threads to finish working; in fact, you set the thread creation time, not the task execution time.
What does the code do?
The executor.execute method executes the provided task inside the thread. Here it takes one of 100 threads and allows it to complete the task.
What happens if there are more than 100 tasks?
With 100 threads, you cannot run more than 100 simultaneous tasks. Other tasks will be queued until one task is completed so that one thread is available for its execution. This is a good way to make sure that you are not creating too many threads and that OutOfMemory or other unpleasant things are not happening.
How many threads should you use?
It depends on the type of tasks you want to complete.
If this looks like a web server, you are usually tied to IO, waiting for the database to receive data, and then for the network to send a response, your stream will basically wait. Thus, even one processor will benefit from a dozen, even a hundred threads. And even if more threads begin to slow down the entire application, it allows you to process a userβs request instead of making it wait or simply refuse a response.
If your task is CPU related, you need something like one task per CPU core to make the most of your hardware, but limit the overhead to context switches. With a 4-core hyper-thread processor, you can go up to 8 simultaneous threads.
This answer is just a brief introduction ... You will find out more by looking at the java.util.concurrent package and read some tutorials.