================ Multi Threading ================ => Multi Threading is used for parallel processing in projects. => Parallel processing means performing multiple tasks at a time. => Multi Threading is mainley used in batch jobs implementation. => Batch jobs are used for batch processing / bulk-operations. Ex: 1) Bank Accounts statements generation (Ex: SBI) 2) Credit-card-bill-generation 3) Post-paid-bill-generation 4) Sending-product-delivery-notifications ==================================================== Monthly Bank Account Statement Generation Scenario ===================================================== public class SBIBank { public void generateBankStatments(){ List list = dao.getAllAccounts(); // 10cr for(Account acc : list){ prepareStmtPdf(acc.getAccNum()); sendStatement(file, acc.getEmail()); } } public void prepareStmtPdf(Long accNum){ // fetch tx info based on acc num from db // generate pdf // store pdf into aws s3 // store stmt details with pdf url in db table } public void sendStatement(File f, String email){ // email sending logic } } => In the above program, we are generating account statements in sequential manner (one after other). Ex : for 1 customer statement generation it is taking 1 sec of time. 1 minute => 60*60 => 60 stmts 1 hour => 60 * 60 => 3,600 statements 1 day => 24 * 3,600 => 86, 400 statements 30 days => 30 * 86, 400 => 25,92,000 statements Note: in SBI, crores of customers having accounts so with above logic to send account statements to all account holders it may take 2 to 3 years of time which is not accepted. Note:: 2025-March bank statement will go customer in 2028 year ====================================== Running our program with 1 thread ====================================== Ex : for 1 customer statement it is taking 1 sec of time. 1 sec => 1 statement 1 minute => 60 * 1 => 60 statements 1 hour => 60 * 60 => 3,600 statements 2 hours => 7,200 statements 1 day => 24 * 3,600 => 86, 400 statements 30 days => 30 * 86, 400 => 25,92,000 statements =================================== Run our program with 10 threads =================================== => If we run our program with 10 threads then in 1 sec 10 statements will be processed. 1 sec => 10 statements 1 min => 60 * 10 => 600 statements 1 hour => 60 * 600 => 36,000 statements 2 hours => 2 * 36, 000 => 72, 000 statements 1 day => 24 * 36,000 => 8, 63, 000 statements =================================== Run our program with 50 threads =================================== => If we run our program with 50 threads then in 1 sec 50 statements will be processed. 1 sec => 50 statements 1 min => 60 * 50 => 3000 statements 1 hour => 60 * 3000 => 1,80,000 statements 2 hours => 2 * 1,80,000 => 3, 60, 000 statements 1 day => 24 * 1,80,000 => 43,20,000 statements ============================================= How to implement Multi Threading in project ============================================= => TO implement Multi Threading we will use Executors Framework public class SBIBank { public void generateBankStatments(){ List list = dao.getAllAccounts(); // 1cr ExecutorService exServ = Executors.newFixedThreadPool(50); for(Account acc : list){ exServ.submit(new Callable(){ public Object call(){ prepareStmtPdf(acc.getAccNum()); sendStatement(file, acc.getEmail()); } }); } } public void prepareStmtPdf(Long accNum){ // fetch tx info based on acc num from db // generate pdf // store pdf into aws s3 // store stmt details with pdf url in db table } public void sendStatement(File f, String email){ // email sending logic } } ===================================== ECommerce Multi Threading Scenario ===================================== public class DeliveryNotifications { public void sendDeliveryNotificationsToAll(){ List orders = dao.getTodayDeliverables(); ExecutorService ex = Executors.newFixedThreadPool(10); for(int i=0; i<=orders.size(); i++){ ex.submit(new Callable(){ public Object call(){ sendNotification(orders.get(i)); } }); } } public static void sendNotification(Order order) { // get order details // get customer details // get address details // send email // send wathsapp msg } }