keepmoving
keepmoving

Reputation: 2043

How multithread works in web environment

I am developing one web application using Spring and Core Java. Every time request comes from client and handled by Spring controller.

@Controller("action")
public class RequestHandler{

@Autowired
private IndialHandler indial;

    @Request("/indialCall")
    public void processWork(HttpServletRequest request){

      String did = request.getParameter("did");
      String resp = indial.dialCall(did);

    }
    }

    public class IndialHandler{

    public String dialCall(String did){
       return checkVoiceCall(did);
    }

    public String checkVoiceCall(String did){

    //here i have a db call, which returns either 1 or 2.

    return service.dbCall(did);

    }


    }

My understanding towards this program is, because every new request create a new Thread in JVM and every thread has its on memory context. That means every thread has its own did and resp variable and no two thread can share the resp variable. That means if i have 1,2,...n different threads and lets assume every thread is generating 1,2,....n response.

Now my question, is it possible in the program thread can come in race condition, and exchange the response message to each other.

Upvotes: 1

Views: 52

Answers (1)

Nathan Hughes
Nathan Hughes

Reputation: 96385

You're close. The servlet container uses a thread pool, it allocates a thread from the pool for each request, and returns the thread to the pool once the response is finished. You don't have to worry about the threads getting mixed up, the servlet container is managing that for you.

Upvotes: 2

Related Questions