================ 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 to customers in ecomm ==================================================== Monthly Bank Account Statement Generation Scenario ===================================================== public class Bank{ public void generateStatementsForAllAccs(){ List accList = dao.getAllAccounts(); // 50 cr for(Account acc : accList){ List txList = dao.getTxHistory(startDate, endDate, acc.getAccNumber()); preparePdfStmt(txList, acc.getAccNum()); } } public void preparePdfStmt(List txList, Long accNum){ // logic to generate pdf // store pdf into s3 bucket // update pdf url in db table sendEmail (filePath); } public void sendEmail(){ // 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 second => 1 statement 1 minute => 60 stmts we can generate 1 hour ==> 60 * 60 ==> 3600 stmts 1 day ==> 24 * 3600 ==> 86, 400 stmts 1 Month ==> 30 * 86,400 ==> 25, 92, 000 stmts 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. ====================================== 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 stmts 1 hour => 60 * 600 => 36,000 stmts 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 ============================================= public class Bank{ public void generateStatementsForAllAccs(){ List accList = dao.getAllAccounts(); // 50 cr ExecutorService exServ = Executors.newFixedThreadPool(20); // threads creation for(Account acc : accList){ exServ.submit(new Callable{ public Object call(){ List txList = dao.getTxHistory(startDate, endDate, acc.getAccNumber()); preparePdfStmt(txList, acc.getAccNum()); } }); } } public void preparePdfStmt(List txList, Long accNum){ // logic to generate pdf // store pdf into s3 bucket // update pdf url in db table sendEmail (filePath); } public void sendEmail(){ // 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 } }