In this lesson, we will see how we can use the executor service in a real-life scenario. We will use the worker threads to execute a task and thus reduce the processing time by using a thread pool.
Suppose you have to send emails to all the students of a college with their details.
The total number of students: 1000 students.
Let's create a single thread using newSingleThreadExecutor() method of ExecutorService. Create a thread which takes 30 milliseconds to execute its task.
Let's create a single thread using newFixedThreadPool(threadCount) method of ExecutorService. Create a thread which takes 30 milliseconds to execute its task.
When a single thread is used to execute task it took more than 30 seconds. We created a thread pool of 10 worker threads, then it took almost 3 seconds. So we can create multiple threads using executor service and can reduce the time and increase the efficiency of a task in the project which requires a similar task to be done multiple times.
Scenario:
Suppose you have to send emails to all the students of a college with their details.
Let's take some assumptions. Consider that it takes around 30 milliseconds to send mail to one student. Let the number of students be 1000. Considering in mind all these total time for sending mail becomes time multiplied with students count plus some delta time for processing extra code.
Assumptions:
The total number of students: 1000 students.
The time it takes to send mail for one student=30 milliseconds or 0.03 second.
Total time using single thread= 0.03 X 1000 + delta time for processing=30 seconds + delta time for processing.
Using Single Thread:
Let's create a single thread using newSingleThreadExecutor() method of ExecutorService. Create a thread which takes 30 milliseconds to execute its task.
class SendMail implements Runnable { public void run() { try{ Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } } } |
Run the task for 3000 times. For running with single thread the code will be like below.
ExecutorService es=Executors.newSingleThreadExecutor();for(int i=0;i<3000;i++){ es.submit(new SendMail()); } |
We can print the date to find the time difference between start and end of the code to send mail.
It almost came out 30 seconds + some delta time due to processing.
Using Thread Pool:
Let's create a single thread using newFixedThreadPool(threadCount) method of ExecutorService. Create a thread which takes 30 milliseconds to execute its task.
class SendMail implements Runnable { public void run() { try{ Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } } } |
Run the task for 3000 times. For running with a thread pool of 10 worker threads, the code will be like below.
ExecutorService es=Executors.newFixedThreadPool(10); for(int i=0;i<3000;i++){ es.submit(new SendMail()); } |
We can print the date to find the time difference between start and end of the code to send mail.
It almost came out 3 seconds using 10 worker threads due to processing.
Conclusion:
When a single thread is used to execute task it took more than 30 seconds. We created a thread pool of 10 worker threads, then it took almost 3 seconds. So we can create multiple threads using executor service and can reduce the time and increase the efficiency of a task in the project which requires a similar task to be done multiple times.
No comments:
Post a Comment