Search code examples
javamultithreadingthread-safetythreadpoolrunnable

How multi-threading can be used in a real time web based application, can i get some scenarios


How multi-threading can be used in a real time web based application, can i get some scenarios.

I am learning Java Multi threading concept, i have worked on java web application.

But never got a chance to work on multi threading, just wanted to know for what kind of operations it can be used.


Solution

  • For example, you are working with an order system. A user might have multiple orders, an order from eBay, another from Amazon. And you need return his/her orders by his id.

    You can get his orders with a single thread:

    List<Order> getOrders(String userId) {
        getEBayOrders(userId);
        getAmazonOrders(urderId);
    }
    

    if getEBayOrders takes 1 second, getAmazonOrders takes 1 second, then you need 2 seconds to return the result.

    With 2 threads, you can call getEBayOrders and getAmazonOrders at the same time, then the user can get his orders in 1 second.